Sorting in multiple columns in gridview using C#
-
Hi, I am using sorting feature in multiple columns in a gridview. My gridview sorting is like this:
protected void gView_Sorting(object sender, GridViewSortEventArgs e)
{
//checking direction for sorting
try
{
if (this.ParentPortlet.PortletViewState["direction"].ToString() == "")
{
this.ParentPortlet.PortletViewState["direction"] = " ASC";
}
else if (this.ParentPortlet.PortletViewState["direction"].ToString() == " ASC")
{
this.ParentPortlet.PortletViewState["direction"] = " DESC";
}
else if (this.ParentPortlet.PortletViewState["direction"].ToString() == " DESC")
{
this.ParentPortlet.PortletViewState["direction"] = " ASC";
}
}
catch
{
this.ParentPortlet.PortletViewState["direction"] = " DESC";
}
getData(e.SortExpression.ToString(), this.ParentPortlet.PortletViewState["direction"].ToString());
}I have four columns in the gridview (Name, Date, Location, Cell) and all these columns are sortable. so If i click name, it sorts in desc order and again if i click it, it will sort in asc order which is great. Similar with the other fields. But my problem is If I click the Name, it will sort in desc order. Now If i click Date, it will sort in asc order. Again If I click the Name, it will sort in desc order. Is there any way we can put these sortings independent to each column. Like, If I click Name, it should sort in desc order. Now If I click date, it should sort in desc order. Now again, If i click Name, it should sort in asc order. Any ideas?
suchita
-
Hi, I am using sorting feature in multiple columns in a gridview. My gridview sorting is like this:
protected void gView_Sorting(object sender, GridViewSortEventArgs e)
{
//checking direction for sorting
try
{
if (this.ParentPortlet.PortletViewState["direction"].ToString() == "")
{
this.ParentPortlet.PortletViewState["direction"] = " ASC";
}
else if (this.ParentPortlet.PortletViewState["direction"].ToString() == " ASC")
{
this.ParentPortlet.PortletViewState["direction"] = " DESC";
}
else if (this.ParentPortlet.PortletViewState["direction"].ToString() == " DESC")
{
this.ParentPortlet.PortletViewState["direction"] = " ASC";
}
}
catch
{
this.ParentPortlet.PortletViewState["direction"] = " DESC";
}
getData(e.SortExpression.ToString(), this.ParentPortlet.PortletViewState["direction"].ToString());
}I have four columns in the gridview (Name, Date, Location, Cell) and all these columns are sortable. so If i click name, it sorts in desc order and again if i click it, it will sort in asc order which is great. Similar with the other fields. But my problem is If I click the Name, it will sort in desc order. Now If i click Date, it will sort in asc order. Again If I click the Name, it will sort in desc order. Is there any way we can put these sortings independent to each column. Like, If I click Name, it should sort in desc order. Now If I click date, it should sort in desc order. Now again, If i click Name, it should sort in asc order. Any ideas?
suchita
Using a Hashtable, or similar structure, to store the sort direction based on column is about the only way to get this done I can think of at the moment
No comment
-
Using a Hashtable, or similar structure, to store the sort direction based on column is about the only way to get this done I can think of at the moment
No comment
-
-
Hi, I am using sorting feature in multiple columns in a gridview. My gridview sorting is like this:
protected void gView_Sorting(object sender, GridViewSortEventArgs e)
{
//checking direction for sorting
try
{
if (this.ParentPortlet.PortletViewState["direction"].ToString() == "")
{
this.ParentPortlet.PortletViewState["direction"] = " ASC";
}
else if (this.ParentPortlet.PortletViewState["direction"].ToString() == " ASC")
{
this.ParentPortlet.PortletViewState["direction"] = " DESC";
}
else if (this.ParentPortlet.PortletViewState["direction"].ToString() == " DESC")
{
this.ParentPortlet.PortletViewState["direction"] = " ASC";
}
}
catch
{
this.ParentPortlet.PortletViewState["direction"] = " DESC";
}
getData(e.SortExpression.ToString(), this.ParentPortlet.PortletViewState["direction"].ToString());
}I have four columns in the gridview (Name, Date, Location, Cell) and all these columns are sortable. so If i click name, it sorts in desc order and again if i click it, it will sort in asc order which is great. Similar with the other fields. But my problem is If I click the Name, it will sort in desc order. Now If i click Date, it will sort in asc order. Again If I click the Name, it will sort in desc order. Is there any way we can put these sortings independent to each column. Like, If I click Name, it should sort in desc order. Now If I click date, it should sort in desc order. Now again, If i click Name, it should sort in asc order. Any ideas?
suchita
Hi, I don't know about GridView. However I can tell you about WinForms' DataGridView: 1. if you provide no code at all, it will sort itself as it should; each column remembers its sort direction, and will toggle it every time you hit the column header without having hit another column header. 2. It does not have a Sorting event (i.e. no pre-Sort), it does have a Sorted event (post-Sort). BTW: GridViewSortingEventArgs.SortDirection tells you how the GridView is going to be sorted. Either it is column-specific or unique for the entire GridView, I don't know (and MSDN seems not to be clear on this). If it is column-specific, you should just use it; if it is a single bool covering all columns, you need to replace it by one bool per column, a HashTable could help you do that. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
Hi, I don't know about GridView. However I can tell you about WinForms' DataGridView: 1. if you provide no code at all, it will sort itself as it should; each column remembers its sort direction, and will toggle it every time you hit the column header without having hit another column header. 2. It does not have a Sorting event (i.e. no pre-Sort), it does have a Sorted event (post-Sort). BTW: GridViewSortingEventArgs.SortDirection tells you how the GridView is going to be sorted. Either it is column-specific or unique for the entire GridView, I don't know (and MSDN seems not to be clear on this). If it is column-specific, you should just use it; if it is a single bool covering all columns, you need to replace it by one bool per column, a HashTable could help you do that. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
you want to hold one boolean flag for every column, but have them structured, not just a bunch of loose flags. There are several ways to do that. One way is by holding an array of four bools, one for each column, and use the column number as the array index. A more elastic approach would rely on a HashTable or better yet a Dictionary<string,bool> where the key could be the column header text (they have to be unique!), and the value is the bool itself. That way you can find the bool for a column by feeding the column header text to the HashTable/Dictionary. BTW: you could use the Column itself as the key, however that may or may not be any easier. :) PS: if you're new to HashTable/Dictionary you should read up on those classes.
Luc Pattyn [My Articles] Nil Volentibus Arduum