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 »

Grid View Paging and Sorting


Posted Date: 08 Nov 2005    Resource Type: Articles    Category: ASP.NET/Web Applications
Author: S Sunil NaiduMember Level: Gold    
Rating: 1 out of 5Points: 10




Gridview Paging and Sorting



Gridview control Displays the values of a data source in a table where each column represents a field and each row represents a record.

Check the sample code below



AllowPaging:

Gets or sets a value indicating whether the paging feature is enabled.

AllowSorting:

Gets or sets a value indicating whether the sorting feature is enabled.

In aspx code please write the gridview as


<asp:GridView
AllowPaging=true
AllowSorting=true
ID="grdView"
runat="server"
AutoGenerateColumns=false
Width="50%"
GridLines="Horizontal"
PageSize =5
OnPageIndexChanging="grdView_PageIndexChanging"
OnSorting ="grdView_OnSorting"
DataKeyNames="CustomerName" HeaderStyle-ForeColor=green HeaderStyle-BackColor=RosyBrown
>

<RowStyle Backcolor="#e1e1ff" />
<Columns>
<asp:TemplateField HeaderText="First Name" SortExpression="CustomerName" HeaderStyle-ForeColor=Green>
<ItemTemplate>
<asp:Label ID="lblFirstName" Text='<%# DataBinder.Eval(Container.DataItem, "CustomerName")%>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City" SortExpression="City" HeaderStyle-Font-Underline=true HeaderStyle-ForeColor=white>
<ItemTemplate>
<asp:Label ID="lblCity" Text='<%# DataBinder.Eval(Container.DataItem, "City") %>' runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>




In Code Behind using the following code......




public partial class GridViewPgSort : System.Web.UI.Page
{
DataSet ds = new DataSet();
DataView dv = new DataView();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
dv = bindgrid();
grdView.DataSource = dv;
grdView.DataBind();
}
}

private DataView bindgrid()
{
String ConnString = "Data Source=localhost;Initial Catalog=DatabaseName;Integrated Security=True";
String StrQuery = "select * from TableName";
SqlDataAdapter sadp = new SqlDataAdapter(StrQuery, ConnString);
sadp.Fill(ds);
if (ViewState["sortExpr"] != null)
{
dv = new DataView(ds.Tables[0]);
dv.Sort = (string)ViewState["sortExpr"];
}
else
dv = ds.Tables[0].DefaultView;
return dv;
}

//Sorting Event:




protected void grdView_OnSorting (Object sender, GridViewSortEventArgs e)
{
ViewState["sortExpr"] = e.SortExpression;
grdView.DataSource = bindgrid();
grdView.DataBind();
}

<H2>// Paging Event
</H2>

protected void grdView_PageIndexChanging(Object sender, GridViewPageEventArgs e)
{
grdView.PageIndex = e.NewPageIndex;
grdView.DataSource = bindgrid();
grdView.DataBind();
}

}








Responses

Author: DotNetGal [ss.mmm]    04 Jan 2008Member Level: Silver   Points : 0
Paging is working fine. Sorting is not working for me.



Author: Suresh    27 Mar 2008Member Level: Silver   Points : 0
It's good and simple to use.Wht i am looking is I got here.Thx alot .


Author: Suresh    25 Apr 2008Member Level: Silver   Points : 0
Thanks for the sample code...
It was simple, clear and really helpful...


Author: Ramya    28 Jul 2008Member Level: Bronze   Points : 1
Its really very helpful.

Thanks for Submitting this article.

It would be helpful,if u submit for Filtering and sorting..

Regards,
Ramya


Author: Zelalem    08 Oct 2008Member Level: Gold   Points : 2
Hi,

It is really very helpful and for those who said either sorting or paging doesn't work, please make sure:OnPageIndexChanging="grdView_PageIndexChanging" OnSorting ="grdView_OnSorting" are included in your:
tag in your hlml file.
Or probably make sure the gridview's ID is grdView.

The article is so useful!
Zelalem


Author: Shivarudrayya    15 Oct 2009Member Level: Bronze   Points : 1
Eventhough I have included OnPageIndexChanging="grdView_PageIndexChanging" OnSorting ="grdView_OnSorting" , I am unable to sort the GridView.
Please help me.


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
GridView Sorting  .  GridView paging and sorting  .  GridView Paging  .  

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: ASP.NET DataGrid Template Columns with Image Button - Master Details View - Part I
Previous Resource: ASP.NET DataGrid Template Columns with Image Button - Master Details View - Part II
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET/Web Applications


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use