DataGrid Sorting Problem
-
Hi, In My Datagrid i have shoen the values from tables like passport no,passenger name,travel date,tour_code. But when i click on the column Heading it is convered to Passport_no(ASc) & againi click to Passport_no(Desc) that is it just changing the heading but not actually ascending the values or descending the values of a column. I have set all sort expression for each column and in onsortcommand i write < #region DataGridSortCommand Event public void doSortDataGridItem(object sender , System.Web.UI.WebControls.DataGridSortCommandEventArgs e) { SortingDataGrid.sortDataGridByValue(dgHistory,e); }//End of this event. #endregion > This is the method i used for the sorting < public string sortDataGridByValue(DataGrid datagrid, System.Web.UI.WebControls.DataGridSortCommandEventArgs e) { oColumnCollection=datagrid.Columns; foreach (DataGridColumn oCol in oColumnCollection) { if(e.SortExpression.CompareTo(oCol.SortExpression)== 0) { oCol.HeaderText=oCol.HeaderText.Replace(" (ASC)","").Replace(" (DESC)",""); if(e.SortExpression.IndexOf(" ASC")>1) { oCol.SortExpression = e.SortExpression.Replace(" ASC", " DESC"); oCol.HeaderText = oCol.HeaderText.Replace(" (ASC)",""); oCol.HeaderText = oCol.HeaderText + " (DESC)"; return oCol.SortExpression; }//End of inner If Block. else if (e.SortExpression.IndexOf(" DESC") > 1 ) { oCol.SortExpression = e.SortExpression.Replace(" DESC", ""); return e.SortExpression; } else { oCol.SortExpression = e.SortExpression + " ASC"; oCol.HeaderText = oCol.HeaderText + " (ASC)"; return oCol.SortExpression; } }//End of if block. }//End of For loop. return ""; }//End of method >
-
Hi, In My Datagrid i have shoen the values from tables like passport no,passenger name,travel date,tour_code. But when i click on the column Heading it is convered to Passport_no(ASc) & againi click to Passport_no(Desc) that is it just changing the heading but not actually ascending the values or descending the values of a column. I have set all sort expression for each column and in onsortcommand i write < #region DataGridSortCommand Event public void doSortDataGridItem(object sender , System.Web.UI.WebControls.DataGridSortCommandEventArgs e) { SortingDataGrid.sortDataGridByValue(dgHistory,e); }//End of this event. #endregion > This is the method i used for the sorting < public string sortDataGridByValue(DataGrid datagrid, System.Web.UI.WebControls.DataGridSortCommandEventArgs e) { oColumnCollection=datagrid.Columns; foreach (DataGridColumn oCol in oColumnCollection) { if(e.SortExpression.CompareTo(oCol.SortExpression)== 0) { oCol.HeaderText=oCol.HeaderText.Replace(" (ASC)","").Replace(" (DESC)",""); if(e.SortExpression.IndexOf(" ASC")>1) { oCol.SortExpression = e.SortExpression.Replace(" ASC", " DESC"); oCol.HeaderText = oCol.HeaderText.Replace(" (ASC)",""); oCol.HeaderText = oCol.HeaderText + " (DESC)"; return oCol.SortExpression; }//End of inner If Block. else if (e.SortExpression.IndexOf(" DESC") > 1 ) { oCol.SortExpression = e.SortExpression.Replace(" DESC", ""); return e.SortExpression; } else { oCol.SortExpression = e.SortExpression + " ASC"; oCol.HeaderText = oCol.HeaderText + " (ASC)"; return oCol.SortExpression; } }//End of if block. }//End of For loop. return ""; }//End of method >
I think the best way to sort something is usually in a DataView. If you current datasource is a datatable then you need to do something like:
DataView dv = dataSet.Tables[0].DefaultView;
//This assumes you only have one table in your datasetdv.Sort = "columnName ASC"; //or you can do "columnName DESC";
datagrid.DataSource = dv;
datagrid.Databind();Hope that helps. Ben