C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » ASP.NET/Web Applications »

Displaying the Records in DataGrid from Ascending to Descending and Viceversa


Posted Date: 14 Jun 2004    Resource Type: Articles    Category: ASP.NET/Web Applications
Author: K Hari PrasadMember Level: Gold    
Rating: 1 out of 5Points: 7



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);
}
}









Responses

Author: critic    14 Jun 2004Member 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 2004Member 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 2004Member 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 2004Member Level: Silver   Points : 0
Hai,

This is very good for those new for .net technologies.

Regard's
Raghu.B


Author: Santhosh    09 Sep 2004Member 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.


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: ViewState and Performance
Previous Resource: ASP.NET Page_Load() event handling for QueryStrings Values
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET/Web Applications


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use