As you can see, the above procedure is generic so that it can be used anywhere. I have therefore included it as part of my EnhanceGrid, as it is applicable in any project developed with ASP.NET.

Icon in the header of the column to display sorting direction

To shows the sorting icon in the header text, I have overridden the following events of the standardGridcontrol which is part of the .NET Framework.

OnRowCreated: This event gets fired when rows are created. Here, if theRowTypeisHeader, then I add a sorting icon by getting the index of the item that gets clicked, and then I call theSortingIconmethod to add the icon in the header row.

protected override void OnRowCreated(GridViewRowEventArgs e)

{

if (e.Row.RowType == DataControlRowType.Header)

{

/* Get sorting column index */

int index = GetSortColumnIndex();

if (index != -1)

{

sortingIcon(index, e.Row);

}

}

}

OnSorting: This event gets called when the user clicks on the header column to sort records of the grid control. As you can seen from the code below, it stores the column sort expression inlblSortExpand stores the sort direction inlblSortDirectionand reverses the currently stored value.

protected override void OnSorting(GridViewSortEventArgs e)

{

try

{

lblSortExp = e.SortExpression;

switch (lblSortDirection)

{

case "asc":

{

lblSortDirection = "desc";

break;

}

case "desc":

case "":

case null:

{

lblSortDirection = "asc";

break;

}

}

BindGrid();

}

catch (Exception ex) { }

}

sortingIcon: This function is used to add sorting icons in the header of the column. In this function, I have created aLabelwhich is added dynamically with the header text in the clicked header column.

private void sortingIcon(int index, GridViewRow row)

{

System.Web.UI.WebControls.Label lblSorting =

new System.Web.UI.WebControls.Label();

if (lblSortDirection == "desc")

{

lblSorting.Text = "<span style=\"font-family:" +

"Marlett; font-weight:bold\">6</span>";

}

else

{

lblSorting.Text = "<span style=\"font-family:Marlett;" +

" font-weight:bold\">5</span>";

}

row.Cells[index].Controls.Add(lblSorting);

}

GetSortColumnIndex: This function is used to get the index of the column which is clicked by the user for sorting. In this function, I compare the sorting expression of the clicked column with each column and get the index of the clicked column. This is needed because I don’t know the index of the clicked column.

private int GetSortColumnIndex()

{

foreach (DataControlField field in this.Columns)

{

if (field.SortExpression.ToString() == lblSortExp)

{

return this.Columns.IndexOf(field);

}

}

return -1;

}

Dropdown in the pager to adjust the number of records per page

For the number of records per page, I have overridden the same method that I overrode to show the sorting icon, but here the condition is changed. I check for the pager type row, as you can see below. In the code below, I have created a dropdown control which contains the per page record number and the attached selected change.

protected override void OnRowCreated(GridViewRowEventArgs e)

{

try

{

if (e.Row.RowType == DataControlRowType.Pager)

{

DropDownList ddl ddlNoPages = new DropDownList();

//adds variants of pager size

ddlNoPages.Items.Add("10");

ddlNoPages.Items.Add("50");

ddlNoPages.Items.Add("100");

ddlNoPages.AutoPostBack = true;

//selects item due to the GridView current page size

ListItem li = ddlNoPages.Items.FindByText(this.PageSize.ToString());

if (li != null)

ddlNoPages.SelectedIndex = ddlNoPages.Items.IndexOf(li);

ddlNoPages.SelectedIndexChanged +=

new EventHandler(ddlNoPages _SelectedIndexChanged);

//adds dropdownlist in the additional cell to the pager table

Table pagerTable = e.Row.Cells[0].Controls[0] as Table;

TableCell cell = new TableCell();

cell.Style["padding-left"] = "50px";

cell.Style["text-align"] = "right";

cell.Controls.Add(new LiteralControl("Page Size:"));

cell.Controls.Add(ddlNoPages);

//e.Row.Cells[0].Controls.Add(cell);

pagerTable.Rows[0].Cells.Add(cell);

}

}

catch (Exception ex)

{

}

}

The following event gets fired when the combobox index gets changed. The code is very simple to understand, so I think there is no need to explain it in detail.

void ddlNoPages_SelectedIndexChanged(object sender, EventArgs e)

{

if (PageSize > int.Parse(((DropDownList)sender).SelectedValue))

IsPagesizeChanged = true;

else

IsPagesizeChanged = false;

PageIndex = 0;

//changes page size

PageSize = int.Parse(((DropDownList)sender).SelectedValue);

//binds data source

BindGrid();

}

Note: There are still some errors in this method in which I am working, and I will provide a full solution for this.

After the above gets done, you require to override the following method of the grid for the custom paging Store Procedure to work. In this method, you have to set the values for the paging datasource properties.

protected override void InitializePager(GridViewRow row,

int columnSpan, PagedDataSource pagedDataSource)

{

try

{

#region code for standard paging

//To set custome paging

pagedDataSource.AllowCustomPaging = true;

//To set total no of records retived

pagedDataSource.VirtualCount = Convert.ToInt32(lbltotal);

//To set current page index

pagedDataSource.CurrentPageIndex = lblpageIndex;

#endregion code for standard paging

base.InitializePager(row, columnSpan, pagedDataSource);

}

catch (Exception ex) { }

}

You can also override the above method to provide custom paging of a different kind as per you client requirements.

Properties of the extended grid

Note that, most of the properties get their value form the ViewState and are set in the ViewState. It is by using the ViewState mechanism that the grid retains property values during postback events.

Another thing to note here is that the control ID is attached with each ViewState property because if you drop the same grid control two or more times on one page, it works property without interfering with other grid operations.

lbltotal: This property of the grid is used to store the total number of records retrieved by the Stored Procedure; it is used to adjust the paging accordingly.

The following code is part of youraspx.csfile. As you can see in the code below, I specifiedSelectList, which is a list of columns;FromClausecontains the table name, which is Employee here;WhereClausehas the filter condition.

For searching purposes, I have added the following controls on the page where I drop the grid control which helps to search a record:

ddlColumn: Which contains the name of the column which is going to be displayed by the EnhanceGrid control. The point to note here is the value field of the the list item which has the names of the columns of the database table.

txtValue: The control which allows the user to enter a value to search for a particular column.

btnSearch: Which contains the code for searching a particular data from a database. As you can see in the code below, if there are values present in thetxtValuetext field, it forms aWhereclause, and if not present, then it passes an emptyWhereclause which searches all the records of the grid control.