Introduction
We can change the order of records from Ascending to Descending and Viceversa when we click on a Column Header of DataGrid by using ViewState Property and SortExpression
The Following small snippet gives the brief idea about this... public class WebForm1 : System.Web.UI.Page { protected System.Web.UI.WebControls.DataGrid DataGrid1; public SqlConnection conn; public SqlCommand cmd; public SqlDataReader reader; private void Page_Load(object sender, System.EventArgs e) { if(!Page.IsPostBack) { /* First Time when the Page is loaded the records are displayed in descending order on column EmpCodeVc */
this.sortCriteria = "EmpCodeVc"; this.sortDir = "desc"; dispgrid(this.sortCriteria,this.sortDir); } }
public void dispgrid(string s1,string s2) { conn = new SqlConnection "server=;database=;uid=;pwd="); conn.Open(); cmd = new SqlCommand ("(select EmpCodeVc,FirstNameVc + ' ' +LastNameVc 'EmpNameVc from employee order by " + s1 +" " + s2 , conn); reader = cmd.ExecuteReader (); DataGrid1.DataSource = reader; DataGrid1.DataBind (); conn.Close(); }
public string sortCriteria { get { /* ViewState is the Property which is used to get or set the values to a Property*/ return (string)ViewState["sortCriteria"]; } set { ViewState["sortCriteria"] = value; } }
public string sortDir { get { return (string)ViewState["sortDir"]; } set { ViewState["sortDir"] = value; } } /* Whenever we Click on DataGrid Column the following Event is going to be executed*/ private void DataGrid1_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e) { if(this.sortCriteria == e.SortExpression) { if(this.sortDir == "desc") this.sortDir = "asc"; else this.sortDir = "desc"; } this.sortCriteria = e.SortExpression; dispgrid(this.sortCriteria,this.sortDir); } }
|
| Author: critic 14 Jun 2004 | Member Level: Bronze Points : 0 |
The indendation used is not correct in this article. In many places, the "{" are not in the same column as the "}".
|
| Author: Sumit Thomas 15 Jun 2004 | Member Level: Silver Points : 0 |
Hi,
Since this code will be useful for beginners, it would be better if you could explain the code or put proper comments in the code to make it easier to understand.
Proper indendation is required.
|
| Author: Rajesh Kumar 16 Jun 2004 | Member Level: Bronze Points : 0 |
HI Your solution is very nice . But one think You must remember For every kind of sorting you are executing the query. So my suggestion is , when You are switching between sorting you dont distrub the database. Use Cache or Xml concept . For further sortings. Thanks By Rajesh
|
| Author: Raghuma 23 Jun 2004 | Member Level: Silver Points : 0 |
Hai,
This is very good for those new for .net technologies.
Regard's Raghu.B
|
| Author: Santhosh 09 Sep 2004 | Member Level: Bronze Points : 0 |
I have to thank you for a lovely description of ur code.It helped me a lot.As I am a fresher to .net field and coding as well,and now presently working on .net. My suggestion would be to give code in this manner to help young people to get a great kind of work experience and pleasure ot be in this field with the help of u experienced guys around. Thank You once again u r doing a great job.
|