Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
hi,
I have used the below code on datagridview1_ColumnHeaderMouseClick
C#
foreach (DataGridViewColumn x in datagridview1.Columns)
            {
                x.SortMode = DataGridViewColumnSortMode.Automatic;
                x.Selected = true;
            }


sorting is not happening ...please help me regarding the same....

even i have tried...
private int Order=-1;

the below code is inside datagridview1_ColumnHeaderMouseClick
ListSortDirection sortDirection;
if (this.datagridview1.SortedColumn.Name == "Number")
            {
                if (this.Order == -1)
                {
                    sortDirection = ListSortDirection.Descending;
                    datagridview1.Sort(datagridview1.Columns["Number"], sortDirection);
                    datagridview1.Columns[e.ColumnIndex].HeaderCell.SortGlyphDirection = System.Windows.Forms.SortOrder.Descending;
                    this.Order = 1;
                }
                else
                {
                    sortDirection = ListSortDirection.Ascending;
                    datagridview1.Sort(datagridview1.Columns["Number"], sortDirection);
                    datagridview1.Columns[e.ColumnIndex].HeaderCell.SortGlyphDirection = System.Windows.Forms.SortOrder.Ascending;
                    this.Order = -1;
                }
            }

even the above code is not working....

[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 10-May-11 4:46am
v5

private const string ASCENDING = " ASC";
private const string DESCENDING = " DESC";
public SortDirection GridViewSortDirection 
{
     	get
     	{
                if (ViewState["sortDirection"] == null)
		ViewState["sortDirection"] = SortDirection.Ascending;
          	return (SortDirection) ViewState["sortDirection"];              
     	}
     	set
	{
		 ViewState["sortDirection"] = value; 
	}
}
protected void GridView_Sorting(object sender,GridViewSortEventArgs e)
{
	 string sortExpression = e.SortExpression;
      if (GridViewSortDirection == SortDirection.Ascending)     
	{
         GridViewSortDirection = SortDirection.Descending;
         SortGridView(sortExpression, DESCENDING);
        } 
      else  
        {
         GridViewSortDirection = SortDirection.Ascending;
         SortGridView(sortExpression, ASCENDING);
      	}
}
private void SortGridView(string sortExpression,string direction) 
{
     //  You can cache the DataTable for improving performance     
	DataTable dt = GetData().Tables[0];
        DataView dv = new DataView(dt);
        dv.Sort = sortExpression + direction;
        GridView1.DataSource = dv;
        GridView1.DataBind();
} 
 
Share this answer
 
Comments
siva455 10-May-11 12:34pm    
I'm using winforms I can't use view state and they were not in sorting order initially
Since nobody wanted to answer this, I thought I'd answer it.

The problem is in the custom binding source. Auto Sort will not work when custom binding source is used.

So you need to remove the line:

datagridview1.Sort(datagridview1.Columns["Number"], sortDirection);


Instead after you fetch data apply sorting logic like this:

private IEnumerable<patient> GetSortedModel(IModel<patient> model, int? sortColumnIndex, ListSortDirection? sortDirection)
       {
           if (sortColumnIndex != null)
           {

               if (sortDirection == ListSortDirection.Ascending)
               {
                   switch (sortColumnIndex)
                   {
                       case 0:
                           return model.GetList().OrderBy(x => x.FacilityName).ThenBy(x => x.Name);
                       case 1:
                           return model.GetList().OrderBy(x => x.ID).ThenBy(x => x.Name);
                       case 2:
                           return model.GetList().OrderBy(x => x.Name).ThenBy(x =>x.Name);
                       ....
                   }
               }
               else
               {
                   switch (sortColumnIndex)
                   {
                       case 0:
                           return model.GetList().OrderByDescending(x => x.FacilityName).ThenBy(x => x.Name);
                       case 1:
                           return model.GetList().OrderByDescending(x => x.ID).ThenBy(x => x.Name);
                       case 2:
                           return model.GetList().OrderByDescending(x => x.Name).ThenBy(x => x.Name);
                       ....
                   }

               }
           }

           return model.GetList().OrderBy(x => x.Name);
       }</patient></patient>


And then apply the datasource.

datagridview1.source = new BindingSource { DataSource = model}; 
 
Share this answer
 

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