Sorting Datagrid
-
Hi All I have a datagrid & I want it to be sorted when the user clicks on the column header..How can I do that?? Thanks a lot in advance :) Happy
-
Hi All I have a datagrid & I want it to be sorted when the user clicks on the column header..How can I do that?? Thanks a lot in advance :) Happy
it's very simple use this function in your class file or at code behind public void BindGrid(DataTable dt, DataGrid dg, string sortexpression, string order) { try { if (dt.Rows.Count > 0) { DataView dv; dv = dt.DefaultView; if (sortexpression != "") { if (order == "desc") dv.Sort = sortexpression + " DESC"; else dv.Sort = sortexpression; } dg.DataSource = dv; dg.DataBind(); } } catch (Exception ex) { clsLog.WriteLog(ex.Message); throw ex; } } where, argument1=datatable object where you hv stored the data which you would like to display 2.argument2=your datagrid id 3.argument3=variable(here u cn take sortexpression which you will declare at the beginning like this private string sortexpression = "";) 4.argument4= Convert.ToString(ViewState["order"]) now on click of column_header event write the code LinkButton lnkbtn; lnkbtn = (LinkButton)sender; if (Convert.ToString(ViewState["order"]) == "Asc") { ViewState["order"] = "desc"; } else { ViewState["order"] = "Asc"; } if (lnkbtn.Text == "1st column headername") { sortexpression = "1st column headername"; } if (lnkbtn.Text == "2ndcolumn headername") { sortexpression = "2ndcolumn headername"; } fillgrid(); (i think u know what to write in this function here just bind ur grid and one more thing in this function call the sort function with required paramts mentioned above) deepti
-
Hi All I have a datagrid & I want it to be sorted when the user clicks on the column header..How can I do that?? Thanks a lot in advance :) Happy
There is an example at : http://www.programmingknowledge.com/dataGridSorting.aspx[^]
-
Hi All I have a datagrid & I want it to be sorted when the user clicks on the column header..How can I do that?? Thanks a lot in advance :) Happy
Change the allowSorting datagrid property to true go to Columns property, select the field what you want to sort, write onto the SortExpression the same column datafield. On datagrid sorting function
protected void Grid_Sorting(object sender, GridViewSortEventArgs e) { string sql = "SELECT * FROM table WHERE conditions ORDER BY " + **e.SortExpression**; SqlDataAdapter data_Adapter = new SqlDataAdapter(sql, conexxxion); DataSet dataSet = new DataSet(); data_Adapter.Fill(dataSet); Grid.DataSource = dataSet.Tables[0].DefaultView; Grid.DataBind(); }
you can do it with all datagrid columns :rose:keep Learning and you never will be out of date...