Wednesday, February 11, 2009

custom gridview sorting using asp.net






As you people know that Gridview is the most powerful functionality of .net. While sorting you can add images and text to your gridview column. If u use Gridview and Object datasource with custom paging then that will increase ur purpormance.. You can also do the same with sqldatasource,linq and entity..
Lets start developing our application..


step1: open visual studio.. click on file->new website->asp.net website-> give the name as customsorting.aspx close the programming language as c#..


step2:In the solution explorer right click the website and add new item.. click on dataset.xsd and name it as employee as i am building it for employee table..


Dataset looks like this..



you can also add multiple table just like linq and entities..
step3: next click on Default.aspx
drag and down gridview on default mode.. go to the design mode.. click on the smart tag click on add new datasouce.. next click on object.. give the connection to employee datatable.. click next and ok.. The code looks like this..







step 4: Now click on Default.cs.. for sorting we need to use Rowdatabound.. For that right click on the gridview go to property and click on rowdatabound.. you will see the code like this..
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
foreach (TableCell cell in e.Row.Cells)
{
LinkButton btn = (LinkButton)cell.Controls[0];
if (btn.Text == GridView1.SortExpression)
{
if (GridView1.SortDirection == SortDirection.Ascending)
{
btn.Text += "desc";
}
else
{
btn.Text += "asc";
}
}
}
}
}

there different properties of gridviewrow such as


1)cells->represents the collection of table row being bound
2)rowtype->represents the type of row being bound..
3)row index->represents the type of index being bound..
4)rowstate->represents the type of state being bound..

step5: set the Default.aspx as startup page and click debug button or F5.. And see the program running..





















1 comment: