• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 282
  • Last Modified:

Sorting on a DataGrid

I have an ASP.Net page written in C# in which I have a datagrid.   I have sorting enabled and created an event handler for the SortCommand.  When I click on the column header to sort the data, the code in the event handler executes but the data in the grid doesn't change.  What have I missed?

public class WebForm1 : System.Web.UI.Page
{
      protected System.Web.UI.WebControls.DataGrid DataGrid1;
      protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
      protected System.Data.SqlClient.SqlCommand sqlSelectCommand1;
      protected System.Data.SqlClient.SqlConnection sqlConnection1;
      protected System.Data.DataView dataView1;
      protected ReverseLinkList.DataSet1 dataSet11;

      private void Page_Load(object sender, System.EventArgs e)
      {
      // Put user code to initialize the page here
      sqlDataAdapter1.Fill(dataSet11,"InTheNews") ;
      DataGrid1.DataBind() ;
      }

      private void DataGrid1_SortCommand(object source,                                                                      
                                                                         System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
      {
      dataView1.Sort = e.SortExpression;
      DataGrid1.DataSource = dataView1 ;
      DataGrid1.DataBind();
      }
0
Mariyam
Asked:
Mariyam
3 Solutions
 
s34nCommented:
i have found a resource that may help

http://www.codeproject.com/csharp/datagridsort.asp
0
 
laotzi2000Commented:
In your design view, you should change the data source property of datagrid1
from dataset11 to dataview.
Then it will work.
0
 
rs_seethaCommented:
dataView1 = new DataView(dataSet11.Tables[0]);
dataView1.Sort = e.SortExpression;

Set the dataview1 to the Datasource table before the sort expresssion.
0
 
MariyamAuthor Commented:
     private void Page_Load(object sender, System.EventArgs e)
{
      // Put user code to initialize the page here
      
      if ( !IsPostBack)
      {
            sqlDataAdapter1.Fill(dsInTheNews1);
            dataView1.Table =  dsInTheNews1.Tables[0] ;
            Cache["dataView1"] = dataView1 ;
            DataGrid1.DataSource = dsInTheNews1 ;
            DataGrid1.DataBind();
      }
}

      private void DataGrid1_SortCommand(object source,
                                                                       System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
      {
            dataView1 = (DataView) Cache["dataView1"] ;
            dataView1.Sort = e.SortExpression ;
            DataGrid1.DataSource = dataView1 ;
            DataGrid1.DataBind();
      }
0
 
MariyamAuthor Commented:
Thanks.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now