RSS

Datagrid Sorting ASP.NET or How to Sort Datagrid


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"


e.g
<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">

AllowSorting="True"
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.
}

PopulateDatagrid()
{
if(sortMode.ToString().Trim().Equals(SORT_ASC))
{
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
}
else
{
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;
dgSearchList.DataBind();

}

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

e.g
<asp:BoundColumn DataField="Location" SortExpression="Location" HeaderText="LocationArea">
<ItemStyle Height="10px" Width="10px"><!--ItemStyle>
asp:BoundColumn>


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.
  1. hyabak

    March 24, 2014 at 12:32 AM

    Hey there! This post could not be written any better! Reading this post
    reminds me of my old room mate! He always kept talking about this.
    I will forward this page to him. Fairly certain he will have a
    good read. Thank you for sharing!

  1. hyabak

    March 24, 2014 at 12:32 AM

    Hey there! This post could not be written any better! Reading this post
    reminds me of my old room mate! He always kept talking about this.
    I will forward this page to him. Fairly certain he will have a
    good read. Thank you for sharing!

Post a Comment

Copyright © Shounak S. Pandit. Powered by Blogger.

Ads