In this article I will explain with an example, how to make use of custom Business Objects (Properties) to bind data to Databound controls like ASP.Net GridView using C# and VB.Net.
Database
Here I am making use of Microsoft’s Northwind Database. You can download it from here.
HTML Markup
The following HTML Markup consists of:
GridView – For displaying data.
The GridView consists of four BoundField columns.
Properties:
AllowPaging – For enabling paging in the GridView control.
Events:
The GridView has been assigned with the following event handlers i.e. OnPageIndexChanging.
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" AllowPaging="true" OnPageIndexChanging="OnPaging">
<Columns>
<asp:BoundField DataField="CustomerId" HeaderText="Customer Id" />
<asp:BoundField DataField="ContactName" HeaderText="Customer Name" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
Business Object Class
The Customer class has following properties.
C#
public class Customer
{
public string CustomerId { get; set; }
public string ContactName { get; set; }
public string City { get; set; }
public string Country { get; set; }
}
VB.Net
Public Class Customer
Public Property CustomerId As String
Public Property ContactName As String
Public Property City As String
Public Property Country As String
End Class
Namespaces
You will need to import the following namespaces.
C#
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data.SqlClient
Binding Custom Business Objects (Properties) to GridView
Inside the Page_Load event handler, the BindGrid method is called.
BindGrid
Inside BindGrid method PopulateData method is called to provide the data to the DataSource property of the GridView.
Finally, the GridView is populated with the records fetched using PopulateData method.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
BindGrid();
}
}
private void BindGrid()
{
gvCustomers.DataSource = this.PopulateData();
gvCustomers.DataBind();
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
BindGrid()
End If
End Sub
Private Sub BindGrid()
gvCustomers.DataSource = Me.PopulateData()
gvCustomers.DataBind()
End Sub
PopulateData
Inside the PopulateData method, the generic List of the Customer class object is created.
The records are fetched using ExecuteReader method, and all the fetched values are set into the respective properties of Customer class properties.
Finally, the generic List of Customer class object is returned.
C#
private List<Customer> PopulateData()
{
List<Customer> customers = new List<Customer>();
string query = "SELECT CustomerID, ContactName, City, Country FROM Customers";
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
customers.Add(new Customer
{
CustomerId = sdr["CustomerID"].ToString(),
ContactName = sdr["ContactName"].ToString(),
City = sdr["City"].ToString(),
Country = sdr["Country"].ToString()
});
}
}
con.Close();
}
}
return customers;
}
VB.Net
PrivateFunction PopulateData() As List(OfCustomer)
Dim customers As List(Of Customer) = New List(Of Customer)()
Dim query As String = "SELECT CustomerID, ContactName, City, Country FROM Customers"
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand(query, con)
con.Open()
Using sdr As SqlDataReader = cmd.ExecuteReader()
While sdr.Read()
customers.Add(New Customer With {
.CustomerId = sdr("CustomerID").ToString(),
.ContactName = sdr("ContactName").ToString(),
.City = sdr("City").ToString(),
.Country = sdr("Country").ToString()
})
End While
End Using
con.Close()
End Using
End Using
Return customers
End Function
Paging in GridView
Inside the OnPageIndexChanging event handler, the PageIndex property of the GridView is updated with the new Page Number which was clicked.
Finally, the GridView is populated using the BindGrid method which in-turn displays the new GridView page.
C#
protected void OnPaging(object sender, GridViewPageEventArgs e)
{
gvCustomers.PageIndex = e.NewPageIndex;
this.BindGrid();
}
VB.Net
Protected Sub OnPaging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
gvCustomers.PageIndex = e.NewPageIndex
Me.BindGrid()
End Sub
Screenshot
Demo
Downloads