?
Solved

Sorting on a DataGrid

Posted on 2005-03-24
5
Medium Priority
?
281 Views
Last Modified: 2008-03-10
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
Comment
Question by:Mariyam
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 1

Assisted Solution

by:s34n
s34n earned 300 total points
ID: 13626838
i have found a resource that may help

http://www.codeproject.com/csharp/datagridsort.asp
0
 
LVL 12

Assisted Solution

by:laotzi2000
laotzi2000 earned 375 total points
ID: 13627120
In your design view, you should change the data source property of datagrid1
from dataset11 to dataview.
Then it will work.
0
 
LVL 1

Accepted Solution

by:
rs_seetha earned 375 total points
ID: 13628191
dataView1 = new DataView(dataSet11.Tables[0]);
dataView1.Sort = e.SortExpression;

Set the dataview1 to the Datasource table before the sort expresssion.
0
 

Author Comment

by:Mariyam
ID: 13639341
     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
 

Author Comment

by:Mariyam
ID: 13639344
Thanks.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question