Building a DataGrid Helper Control For ASP.NET 1.x: Part 1
page 2 of 5
by Li Chen
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 25854/ 83

A Typical Read-Only Data Access Page

[Download Code]
We will first look at what is involved in creating a typical data access page with sorting and paging functionality by examining an example. A live demo can be found at
http://www.dotneteer.com/projects/DataGridHelper/v1/TypicalDataAccessPage.aspx.

The following are the basic steps used to create the page:

  1. Create a SqlConnection and a SqlDataAdapter member: Drag a table from the Server Explorer into the WebForm design surface. This will create a SqlConnection and a SqlDataAdapter member in the web form. Give these members names that make sense. This is one of the quickest ways to create a SqlDataAdapter with simple select, insert, delete and update capabilities. In this article, we will only use the select capability. In the next article of this series, we will also make use the delete and update capabilities.
  2. Add a DataGrid Control: Drag a DataGrid control from the tool box into the WebForm design surface. Enable sorting by setting the AllowSorting property to True and enable paging by setting the AllowPaging property to True. 
  3. Add code to respond to the Page_Load, DataGrid SortCommand and DataGridProducts PageIndexChanged events. The code behind the page will look like:


private void Page_Load(object sender, System.EventArgs e)
{
 if (!this.IsPostBack)
 {
  bindGrid();
 }
}


private void bindGrid()
{
 DataSet ds = new DataSet();
 sqlDataAdapterProducts.Fill(ds);
 DataView dv = ds.Tables[0].DefaultView;
 dv.Sort = (string)this.ViewState["SortExpression"];
 dataGridProducts.DataSource = dv;
 dataGridProducts.DataBind();
}


private void dataGridProducts_SortCommand(object source, 
System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
 string sortExpression = e.SortExpression;
   
 //Alternate the sorting direction
 if ((string)this.ViewState["SortExpression"] == sortExpression)
  sortExpression += " DESC";


 this.ViewState["SortExpression"] = sortExpression;
 
 //Reset CurrentPageIndex on resort
 dataGridProducts.CurrentPageIndex = 0;
   
 bindGrid();
}


private void dataGridProducts_PageIndexChanged(object source, 
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
 dataGridProducts.CurrentPageIndex = e.NewPageIndex;
 bindGrid();
}

The bindGrid function is the function that creates the data source and binds it to the DataGrid.

The code in the Page_Load event will populate the DataGrid when the page is initially accessed. The line, if (!this.IsPostBack), will prevent the bindGrid function from getting called multiple times in a post-back situation such as sorting or paging.

The code in the SortCommand and PageIndexChanged event handlers are responsible for sorting and paging the data. In the code, we also added the functionality alternate the sort direction on subsequent click of the same column header.

Note that the code in the three event handlers is rather generic; the knowledge of the data source is fully contained in the bindGrid function. Therefore, the repetitive code in these three event handlers could be reused by moving it to a control. We will examine how to do this in the next section.


View Entire Article

User Comments

Title: DataGrid   
Name: Anita
Date: 2007-07-30 2:01:28 PM
Comment:
Very Good
Title: Confusing   
Name: Anees
Date: 2006-10-05 1:04:44 AM
Comment:
Its confusing for Begginers.
Title: Atish.netExpress   
Name: Atish J
Date: 2006-09-01 6:05:32 AM
Comment:
Ver nice .......!
Title: datagrid   
Name: shailendra
Date: 2006-09-01 2:18:27 AM
Comment:
good
Title: Good   
Name: Prashant Raizada
Date: 2006-07-27 4:40:54 AM
Comment:
Good job!!!
Title: Testing Form   
Name: BinDev1998
Date: 2004-09-21 11:53:35 PM
Comment:
This is Cool!
Title: good!   
Name: Region
Date: 2004-08-09 1:59:31 AM
Comment:
Is Like My Grid
Title: NICE   
Name: Raghuraman
Date: 2004-08-07 3:12:19 AM
Comment:
a nice walk thru
Title: Not Bad   
Name: Tarzan from Zimbava
Date: 2004-08-07 2:46:39 AM
Comment:
It's a good article for Asp starters
Title: good   
Name: gayathri
Date: 2004-08-07 1:41:33 AM
Comment:
good
Title: good   
Name: ramu
Date: 2004-06-28 4:15:35 AM
Comment:
good article

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-19 3:33:44 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search