DataGrid Sorting Problem
-
Hi ALL of you. i use view state for sorting code is given but problem is that it sorts perfectly on name but not on date .. What to do???? actually i chage the date into varchar in order to display in drid For Ex it shows 01 Feb 2007 in Grid but when i click on header it dont sort in correct way..
Bind(); oColumnCollection=dgHistory.Columns; if(ViewState["SortOrder"].ToString() == "" || ViewState["SortOrder"].ToString() == "DESC") { dv.Sort = e.SortExpression; // oCol.HeaderText = oCol.HeaderText + " (ASC)"; ViewState["SortExpression"]= e.SortExpression + "ASC"; ViewState["SortOrder"] = "ASC"; } else { dv.Sort = e.SortExpression + " DESC"; // oCol.HeaderText = oCol.HeaderText + " (DESC)"; ViewState["SortExpression"]= e.SortExpression + " DESC"; ViewState["SortOrder"] = "DESC"; } dgHistory.DataBind();
Thanks -
Hi ALL of you. i use view state for sorting code is given but problem is that it sorts perfectly on name but not on date .. What to do???? actually i chage the date into varchar in order to display in drid For Ex it shows 01 Feb 2007 in Grid but when i click on header it dont sort in correct way..
Bind(); oColumnCollection=dgHistory.Columns; if(ViewState["SortOrder"].ToString() == "" || ViewState["SortOrder"].ToString() == "DESC") { dv.Sort = e.SortExpression; // oCol.HeaderText = oCol.HeaderText + " (ASC)"; ViewState["SortExpression"]= e.SortExpression + "ASC"; ViewState["SortOrder"] = "ASC"; } else { dv.Sort = e.SortExpression + " DESC"; // oCol.HeaderText = oCol.HeaderText + " (DESC)"; ViewState["SortExpression"]= e.SortExpression + " DESC"; ViewState["SortOrder"] = "DESC"; } dgHistory.DataBind();
ThanksFor date sorting, you have to use some different logic, especially date sorting is the best one. But, here you are using just string sorting, so you won't get correct result. So, use date field in the dataview instead of changing into varchar (that is best way), and use format to display the date into the specified format. You can use the DataFormatString property in grid. For example: for bound column,
....asp:BoundColumn DataField="FieldName" DataFormatString="{0:dd MMM yyyy}" .....
I hope, you can get the answer from this.Regards R.Arockiapathinathan
-
Hi ALL of you. i use view state for sorting code is given but problem is that it sorts perfectly on name but not on date .. What to do???? actually i chage the date into varchar in order to display in drid For Ex it shows 01 Feb 2007 in Grid but when i click on header it dont sort in correct way..
Bind(); oColumnCollection=dgHistory.Columns; if(ViewState["SortOrder"].ToString() == "" || ViewState["SortOrder"].ToString() == "DESC") { dv.Sort = e.SortExpression; // oCol.HeaderText = oCol.HeaderText + " (ASC)"; ViewState["SortExpression"]= e.SortExpression + "ASC"; ViewState["SortOrder"] = "ASC"; } else { dv.Sort = e.SortExpression + " DESC"; // oCol.HeaderText = oCol.HeaderText + " (DESC)"; ViewState["SortExpression"]= e.SortExpression + " DESC"; ViewState["SortOrder"] = "DESC"; } dgHistory.DataBind();
ThanksHi, Do not covnert you date column to varchar but set the DataFormatString property of the column in which you are displaying the date value. Design Time I hope this will help you. Thanks and Regards, Chetan Ranpariya