Introduction:
The DataGrid control was large enough to contain and display all the items in the data source. In a real-world scenario, though, a couple of additional functions are often needed—paging and sorting. The DataGrid control provides good built-in support for both functions.
Main:
Paging:
The DataGrid control provides some built-in facilities to let the programmer easily switch to a new page according to the user’s clicking. The control needs to know how many items should be displayed per page, what type of functionality is required for the pager, and the data source to page through. In return for this, the control tracks the current page index, extracts the rows that fit into the particular page, and refreshes the user interface. Whenever the page index changes, an event is fired to the application—the PageIndexChanged event.
<asp:datagrid runat=”server” id=”grid”
allowpaging=”true”
onpageindexchanged=”PageIndexChanged”>
Common Code for datagrid paging,
void PageIndexChanged(object sender, DataGridPageChangedEventArgs e) {
grid.CurrentPageIndex = e.NewPageIndex;
BindData();
}
We can esaily customizing paging in datagrid,
<pagerstyle backcolor=”gray” forecolor=”white” font-name=”webdings”
font-size=”12pt” prevpagetext=”3″ nextpagetext=”4″ />
Otherwise set a style in databound,
if (e.Item.ItemType == ListItemType.Pager) {
if (Grid.PagerStyle.Mode == PagerMode.NextPrev) {
TableCell pager = e.Item.Cells[0];
Label ctl = new Label();
ctl.Font.Name = “verdana”;
ctl.Font.Size = FontUnit.Point(8);
ctl.Text = String.Format(“ <b>Page {0}</b> ”,
1 + cartGrid.CurrentPageIndex);
pager.Controls.AddAt(1, ctl);
}
}
Sorting:
To enable the DataGrid’s sorting capabilities, you set the AllowSorting property to true. When sorting is enabled, the DataGrid gains the ability of rendering the header text of columns as links. You can associate each column with a sorting expression by using the SortExpression property. A sorting expression is any comma-separated sequence of column names. Each column name can be enriched with an order qualifier such as DESC or ASC. DESC indicates a descending order, while ASC denotes the ascending order. The ASC qualifier is the default; if omitted, the column is sorted ascendingly.
<columns>
<asp:buttoncolumn runat=”server”
DataTextField=”Customername”
HeaderText=”Product”
SortExpression=”Customername” />
</columns>
void SortCommand(object sender, DataGridSortCommandEventArgs e) {
ViewState["DataSortExpression"] = e.SortExpression;
BindData();
}
void BindData() {
DataTable data = (DataTable) Cache["AppData"];
DataView dv = data.DefaultView;
dv.Sort = (string) ViewState["DataSortExpression"];
grid.DataSource = dv;
grid.DataBind();
}
Conclusion:
Hope,this helps,
Happy Coding.
interesting blog. It would be great if you can provide more details about it. Thanks you