In this article, I’ll explain how to make use of Custom Business Objects to bind data to Databound controls like the ASP.Net GridView Control.
Database
Here I am making use of Microsoft’s Northwind Database. The download and install instructions are provided in the following article.
Download and install Northwind Database
HTML Markup
The following HTML Markup consists of an ASP.Net GridView Control which will be populated using Generic List of Custom Business Objects.
<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 = "Contact Name" />
<asp:BoundField DataField = "City" HeaderText = "City" />
<asp:BoundField DataField = "Country" HeaderText = "Country" />
</Columns>
</asp:GridView>
Business Object Class
The following class named Customer will be used to populate the GridView control with records from database table. The class has
four properties i.e. CustomerId, ContactName, City and Country.
C#
public class Customer
{
string _customerId;
public string CustomerId
{
get
{
return _customerId;
}
set
{
_customerId = value;
}
}
string _contactName;
public string ContactName
{
get
{
return _contactName;
}
set
{
_contactName = value;
}
}
string _city;
public string City
{
get
{
return _city;
}
set
{
_city = value;
}
}
string _country;
public string Country
{
get
{
return _country;
}
set
{
_country = value;
}
}
}
VB.Net
Public Class Customer
Private _customerId As String
Private _contactName As String
Private _city As String
Private _country As String
Public Property CustomerId() As String
Get
Return _customerId
End Get
Set(ByVal value As String)
_customerId = value
End Set
End Property
Public Property ContactName() As String
Get
Return _contactName
End Get
Set(ByVal value As String)
_contactName = value
End Set
End Property
Public Property City() As String
Get
Return _city
End Get
Set(ByVal value As String)
_city = value
End Set
End Property
Public Property Country() As String
Get
Return _country
End Get
Set(ByVal value As String)
_country = value
End Set
End Property
End Class
Populating the Custom Business Objects
Inside the following function, the records from the
Customers table of the Northwind Database are fetched using DataReader and
inside the WHILE loop a Generic List consisting of the objects of the Customer
class is populated.
C#
private List<Customer> PopulateData()
{
string constring = ConfigurationManager.ConnectionStrings["constring"].ConnectionString;
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("SELECT *
FROM Customers", con))
{
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
List<Customer> customers = new List<Customer>();
while (sdr.Read())
{
customers.Add(new Customer());
customers[customers.Count - 1].CustomerId = sdr["CustomerID"].ToString();
customers[customers.Count - 1].ContactName = sdr["ContactName"].ToString();
customers[customers.Count - 1].City = sdr["City"].ToString();
customers[customers.Count - 1].Country = sdr["Country"].ToString();
}
con.Close();
return customers;
}
}
}
}
VB.Net
Private Function PopulateData() As List(Of Customer)
Dim constring As String = ConfigurationManager.ConnectionStrings("constring").ConnectionString
Using con As SqlConnection = New SqlConnection(constring)
Using cmd As SqlCommand = New SqlCommand("SELECT *
FROM Customers", con)
con.Open()
Using sdr As SqlDataReader = cmd.ExecuteReader()
Dim customers As New List(Of Customer)
While sdr.Read
customers.Add(New Customer)
customers((customers.Count - 1)).CustomerId = sdr("CustomerID").ToString
customers((customers.Count - 1)).ContactName = sdr("ContactName").ToString
customers((customers.Count - 1)).City = sdr("City").ToString
customers((customers.Count - 1)).Country = sdr("Country").ToString
End While
con.Close()
Return customers
End Using
End Using
End Using
End Function
Populating the GridView control
The following function populates the GridView control with the Generic List of Customer class objects.
C#
private void BindGrid(List<Customer> customers)
{
gvCustomers.DataSource = customers;
gvCustomers.DataBind();
}
VB.Net
Private Sub BindGrid(ByVal customers As List(Of Customer))
gvCustomers.DataSource = customers
gvCustomers.DataBind()
End Sub
The above method is called inside the Page_Load event of the ASP.Net page in the following way.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindGrid(this.PopulateData());
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Me.BindGrid(Me.PopulateData())
End If
End Sub
Paging in GridView
Inside the OnPageIndexChannging event handler, the new PageIndex is set and the GridView is populated using the Generic List of Customer class objects.
C#
protected void OnPaging(object sender, GridViewPageEventArgs e)
{
gvCustomers.PageIndex = e.NewPageIndex;
this.BindGrid(this.PopulateData());
}
VB.Net
Protected Sub OnPaging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
gvCustomers.PageIndex = e.NewPageIndex
Me.BindGrid(Me.PopulateData)
End Sub
Downloads
Download Code