Click here to Skip to main content
12,291,499 members (68,409 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: SQL-server-2005 C# ASP.NET , +
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.

 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 29-Apr-12 23:39pm
Edited 29-Apr-12 23:41pm
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

It looks like you need to have
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
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[^].
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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;
}
  Permalink  
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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

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...
  Permalink  
v3

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160518.1 | Last Updated 14 Jun 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100