Datagrid sorting
-
In my application, there is a datagrid control and there are several columns on it. You know that datagrid control applies sorting data but I want to sort as multiple columns. For example, two columns are date and username. Firstly datagrid sort by date and after sort by username. Sorting priority of date greater than username. Date UserName 12.03.07 Mehmet 12.01.06 John 24.05.08 Michael 24.05.08 Anna 12.03.07 Becham Sorting by only date Date UserName 12.01.06 John 12.03.07 Mehmet 12.03.07 Becham 24.05.08 Michael 24.05.08 Anna Sorting by firstly date and after, by UserName Date UserName 12.01.06 John 12.03.07 Becham 12.03.07 Mehmet 24.05.08 Anna 24.05.08 Michael Thanks for your helps.
-
In my application, there is a datagrid control and there are several columns on it. You know that datagrid control applies sorting data but I want to sort as multiple columns. For example, two columns are date and username. Firstly datagrid sort by date and after sort by username. Sorting priority of date greater than username. Date UserName 12.03.07 Mehmet 12.01.06 John 24.05.08 Michael 24.05.08 Anna 12.03.07 Becham Sorting by only date Date UserName 12.01.06 John 12.03.07 Mehmet 12.03.07 Becham 24.05.08 Michael 24.05.08 Anna Sorting by firstly date and after, by UserName Date UserName 12.01.06 John 12.03.07 Becham 12.03.07 Mehmet 24.05.08 Anna 24.05.08 Michael Thanks for your helps.
I think you can sort by the following codes. // Only UserName MyDateGrid.Source = MyDataView; MyDataGrid.Sort = "UserName"; MyDataGrid.DataBind(); // First UserName than date: MyDateGrid.Source = MyDataView; MyDataGrid.Sort = "UserName, theDate"; MyDataGrid.DataBind(); // First date than UserName: MyDateGrid.Source = MyDataView; MyDataGrid.Sort = "theDate, UserName"; MyDataGrid.DataBind(); You also need a property that knows what the last sorthing method was. private string lastSortCommand = "";
if(lastSortCommand == "UserName" && currentSortCommand == "theDate") { MyDateGrid.Source = MyDataView; MyDataGrid.Sort = "theDate, UserName"; MyDataGrid.DataBind(); } else if(lastSortCommand == "theDate" && currentSortCommand == "UserName") { MyDateGrid.Source = MyDataView; MyDataGrid.Sort = "UserName, theDate"; MyDataGrid.DataBind(); } else { MyDateGrid.Source = MyDataView; MyDataGrid.Sort = currentSortCommand; MyDataGrid.DataBind(); } lastSortCommand = currentSortCommand;
Haven't tryed it out, but I'll think i works.