Datagrid Sorting ASP.NET or How to Sort Datagrid

10 Sep

Datagrid Sorting ASP.NET or How to Sort Datagrid

Step I )

In the Datagrid definition:
add 2 properties AllowSorting = true and OnSortCommand=”MethodName in code behind”

<asp:datagrid id=”dgSearchList” runat=”server” Height=”125px” Width=”627px” CssClass=”panelChildSolidBorder”
CellPadding=”2″ AllowCustomPaging=”True” AutoGenerateColumns=”False” OnItemCommand=”detailsClicked”
ShowHeader=”True” AllowSorting=”True” OnSortCommand=”dgSearchList_SortClick” PageSize=”4″>

makes the datagrid sortable and OnSortCommand=”dgSearchList_SortClick” tells which method to call when the header is clicked.

Step II )

Now we need to define the method to call when the column headers are clicked
here dgSearchList_SortClick is the method that will be called when the Column headers are clicked for sorting.

Codebehind :

public void dgSearchList_SortClick(object sender,DataGridSortCommandEventArgs e)
sortField = e.SortExpression; // ie the Sortexpression assigned to the Column.Check STEP III for how to assign a   // sortexpression on a column.
PopulateDatagrid(); //Call the method that populates the Datagrid with
//the values from the Dataview.

sortMode = SORT_DESC; // Here sortMode is just a Variable storing the
direction of the sort.There are better ways to store this than the current one shown hereJ
sortMode = SORT_ASC;
txtSortMode.Text = sortMode;

// SORT_DESC and SORT_ASC are constants with the following string values.
// SORT_DESC = “Desc”
// SORT_ASC = “Asc”

DataView dv = new DataView(dt);
dv.Sort = sortField.Trim() + ” ” + sortMode;
dgSearchList.DataSource = dv;


The code above checks whether we want to sort in Ascending order or in Descending order
and assigns the SortExpression concatenated with the Sorting direction (Asc/Desc)
and binds the datagrid again (don’t forget to bind the data grid its very important)

Step III)
You need to specify the sorting expression for that column in the column definition in the ASPX page

<asp:BoundColumn DataField=”Location” SortExpression=”Location” HeaderText=”LocationArea”>
<ItemStyle Height=”10px” Width=”10px”><!–ItemStyle>

be carefull of what you define as the sortexpression because this is what is passed to the codebehind and sortexpression is what identifies which column was clicked for sorting

in this case the sort expression for the LocationArea column is “Location”, hence when we clicking on the location column will set the e.SortExpression (check Step III for e.SortExpression) to “Location”. Hence identifying the column clicked.

Leave a comment

Posted by on September 10, 2008 in .NET, Code Snippets, Problem Solving


Tags: , , ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: