C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Interview   Jobs   Projects   Offshore Development    
Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing | Talk to Us |



My Profile

Gifts

Active Members
TodayLast 7 Days more...







Grid View Paging and Sorting


Posted Date: 08 Nov 2005    Resource Type: Articles    Category: Web Applications

Posted By: S Sunil Naidu       Member Level: Gold
Rating:     Points: 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


Feedbacks      
Popular Tags   What are tags ?   Search 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: Universal Search Engine for MS Access database
Previous Resource: Handling SelectedIndexChanged event in DropDownList
Return to Discussion Resource Index
Post New Resource
Category: Web Applications


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

web conferencing

Contact Us    Privacy Policy    Terms Of Use