Recently i have stucked when working with the grid view.i.e sorting the data in grid view using c sharp.Here i will shown how sort the data in grid view.For this i will give brief explanation about sorting information by clicking on column header of grid view.Here i have taken one grid view with fallowing properties.By using htis example we can sort the data using gridview header
Grid view Properties:
Allow sorting-->True:-column headings will be provided with hyperlinks[link button]
Code behind:
When user clicks on the column header of grid view post back takes place,sorting event procedure of grid view will be executed.This Event procedure will be provided column name
Here i will shown how to high lighting the emp name according to country.This requires Row data bound event of grid view control.Row data bound event will be executed towards each row construction with data from data source.This will provide access to row constructed
Grid view Properties:
Allow sorting-->True:-column headings will be provided with hyperlinks[link button]
Code behind:
//pageload
if(page.ispostback==false)
{
//ordinary request information will be displayed sorted based on the empname
Sqlconnection con=New Sqlconnection("userid=sa";password=;databse=emp");
SqlDataadaptor da=New SqlDataadapator("select*from Employee Orderby empname",con);
//orderby is used to retrieve records in sorted order
Dataset ds=New Dataset();
da.fill(ds,"Employee");
Gvemp.Datasource=ds.Table["Employee"];
Gvemp.DataBind();
}
Providing Logic For Grid view Event:-When user clicks on the column header of grid view post back takes place,sorting event procedure of grid view will be executed.This Event procedure will be provided column name
protected void Gridview-sorting(Object sender,Event Args e)
{
Response.Write{"colname:"+e.SortExpression);
//e.SortExpression will provide colomn name selected by user
SqlDataadaptor da=New SqlDataadapator("select*from Employee Orderby empname"+e.Sortexpression,con);
Dataset ds=New Dataset();
da.fill(ds,"Employee");
Gvemp.Datasource=ds.Table["Employee"];
Gvemp.DataBind();
}
Note:The similar code is required in page load and Grid view-sorting event Process .To avoid repetition using fallowing subprogram to fill grid view void fillgrid(cname);
Here i will shown how to high lighting the emp name according to country.This requires Row data bound event of grid view control.Row data bound event will be executed towards each row construction with data from data source.This will provide access to row constructed
protected void Gridview-rowdataBound(..,..)
{
Response.Write(e.row.cells[4].Text+"
");
if(e.Row.cells[4].Text=="Usa")
e.Row.Backcolor=system.drawing.color.Red;
}
No comments:
Post a Comment