Anggur Solutions Anggur Solutions - 2 months ago 31
C# Question

Sorting gridview column in asp.net

I have a gridview that loads data from database. The data is displayed in the gridview for the first time sorted by datetime. On one column of that gridview I want to allow sorting, for example on the main_post column. I had tried this, but the gridview still did not sort when I click the header of the column.

This is my front-end code for gridview:

<asp:DataGrid ID="Datagrid1" runat="server"
AllowPaging="True"
AllowSorting="True"
OnSorting="ComponentGridView_Sorting">
<Columns>
<asp:BoundColumn DataField="main_post" HeaderText="Header Post ID"
SortExpression="ComponentGridView_Sorting" />
</Columns>
</asp:DataGrid>


My back-end code:

protected void loadData()
{
DataTable dtTemp;
dtTemp = objDBInterface.getResults(strSQL);
Datagrid1.DataSource = dtTemp;
Datagrid1.DataBind();
ViewState["dtbl"] = dtTemp;
}

protected void ComponentGridView_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dataTable = ViewState["dtbl"] as DataTable;
if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
dataView.Sort = e.SortExpression + " " + ConvertSortDirection(e.SortDirection);
Datagrid1.DataSource = dataView;
Datagrid1.DataBind();
}
}
private string ConvertSortDirection(SortDirection sortDirection)
{
string newSortDirection = String.Empty;
switch (sortDirection)
{
case SortDirection.Ascending:
newSortDirection = "ASC";
break;
case SortDirection.Descending:
newSortDirection = "DESC";
break;
}

return newSortDirection;
}


I dont have any error and I dont know where my mistake is, could somebody please help me to solve this problem?

Thanks in advance.

Answer

I got the solution. Hope this will help others people that face this similar problem. On .aspx page need to change:

from OnSorting="ComponentGridView_Sorting"

to onsortcommand="ComponentGridView_Sorting"

Then put this code on aspx.cs(back-end code):

protected void Datagrid1_SortCommand(object source, DataGridSortCommandEventArgs e)
    {
        string strSQL;
        DataTable dt;
        strSQL = "YOUR SELECT STATEMENT (SQL)";
        dt = strSQL;
            {
                string SortDir = string.Empty;
                if (dir == SortDirection.Ascending)
                {
                    dir = SortDirection.Descending;
                    SortDir = "Desc";
                }
                else
                {
                    dir = SortDirection.Ascending;
                    SortDir = "Asc";
                }
                DataView sortedView = new DataView(dt);
                sortedView.Sort = e.SortExpression + " " + SortDir;
                Datagrid1.DataSource = sortedView;
                Datagrid1.DataBind();
            }
    }

    protected SortDirection dir
    {
        get
        {
            if (ViewState["dirState"] == null)
            {
                ViewState["dirState"] = SortDirection.Ascending;
            }
            return (SortDirection)ViewState["dirState"];
        }
        set
        {
            ViewState["dirState"] = value;
        }
    }