Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
Hi Frnds, This is Ranjith Rdy.

I have a gridview on my webform and finally loaded data into it.
Now i need to sort it when i click on the header columns in gridview.

I have five fields, I need to SORT it using Header columns.
and i gave Allow Sorting = TRUE


This is my code.

C#
 protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {      
        DataTable dtSortTable = GridView1.DataSource as DataTable;

        if (dtSortTable != null)
        {
            DataView dvSortedView = new DataView(dtSortTable);

            dvSortedView.Sort = e.SortExpression + "" + getSortDirectionString(e.SortDirection);
        
            GridView1.DataSource = dvSortedView;
            GridView1.DataBind();
        }
    }

private string getSortDirectionString(SortDirection sortDirection)
    {
        string newSortDirection = String.Empty;
        if(sortDirection== SortDirection.Ascending)
        {
            newSortDirection = "ASC";
        }
        else
        {
            newSortDirection = "DESC";
        }
        return newSortDirection;
}



This is my Above code...its unable to work out..
Please anyone can suggest me and steps for SORTING.

Thanks ,
Posted
Updated 29-Mar-17 3:22am
v2
Comments
kartheeee 23-Jul-12 14:23pm    
hi, i am trying same code, but datatable always show null
how to solve?

protected void SortRecords(object sender, GridViewSortEventArgs e)
{
DataTable dt1 = GridView1.DataSource as DataTable;
if (dt1 != null)
{
DataView dv = new DataView(dt1);
if (e.SortDirection == SortDirection.Ascending)
{
dv.Sort = e.SortExpression + "" + "desc";
}
else
{
dv.Sort = e.SortExpression + " " + " asc";
}
GridView1.DataSource = dv;
GridView1.DataBind();
}

tks
kartheeee

It looks like you need to have
C#
var DataSource = from data in GridViewData
                    orderby data.Column1
                    select data;

Since the orderby clause takes a property name. The clause doesn't parse the string to determine what to order by like Dynamic Queryable does.

Or, in the case of sorting on multiple fields, I would use
C#
var DataSource = from data in GridViewData
                    select data;
switch(fieldToSortOn)
{
  case "Column1":
    DataSource = NewDataSource.OrderBy(x => x.Field1);
    break;
  case "Column2":
    DataSource = NewDataSource.OrderBy(x => x.Field2);
    break;

  ...
}
GridView1.DataSource = DataSource; 
GridView1.DataBind();


You can also try using Handle GridView.OnSorting() and create sorting expression dynamically using LINQ[^].
 
Share this answer
 
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt=getdata();//here getdata() method returns data from database;
        Session["data"] = dt;
    }
}

protected void ComponentGridView_Sorting(object sender, GridViewSortEventArgs e)
{
    DataTable dataTable = Session["data"] as DataTable;

    if (dataTable != null)
    {
        DataView dataView = new DataView(dataTable);
        dataView.Sort = e.SortExpression + " " + ConvertSortDirection(e.SortDirection);

        ComponentGridView.DataSource = dataView;
        ComponentGridView.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;
}
 
Share this answer
 
v2
Comments
Dhirendra Kumar 18-Oct-13 4:33am    
it works..Thank you very much..
Commish13 22-Nov-13 19:33pm    
I'm using the code above but I think I'm put the wrong value in this line here:
dt=getdata();//here getdata() method returns data from database;
I put
Dim dt As New DataTable()
grdEmp.DataSource = dt
Session("data") = dt

Whenever I run my program I get an error message saying it can't find the Column(Which ever Column header I click on)

I'm very new at this so could someone let me know exactly I should type in there.
Hope this may helps You...Dont forget to mark as Answer if this is the right one..k...
 protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dtbl = new DataTable();
         dtbl = ;//here get the datatable from db
        if (ViewState["Sort Order"] == null)
        {
            dtbl.DefaultView.Sort = e.SortExpression+" DESC";
            GridView1.DataSource = dtbl;
            GridView1.DataBind();
            ViewState["Sort Order"] = "DESC";
        }
        else
        {
            dtbl.DefaultView.Sort = e.SortExpression + "" + " ASC";
            GridView1.DataSource = dtbl;
            GridView1.DataBind();
            ViewState["Sort Order"] = null;
        }
}

Happy Codding...
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900