ARTICLE

Grid View Paging and Sorting in ASP.Net

Posted by Jagdeep Singh Articles | ASP.NET Programming November 02, 2010
In this article you will learn how to create Grid View Paging and Sorting in ASP.Net
Reader Level:

Step 1. Create new project in visual studio.

Step 2. Open default.aspx and drag and drop GridView control in design view.

Step 3. Open default.aspx.cs and add following line in using directives:

using System.Data.SqlClient;

Step 4. Open Web.config and add following line just above <system.web>:

<connectionStrings>
    <
add name="con" connectionString="Data Source=.\sqlexpress;initial catalog=database_name;integrated security=true;" providerName="System.Data.SqlClient"/>
  </connectionStrings>

Step 5. Open properties of GridView, change AllowSorting and AllowPaging property to True.

Step 6. Open default.aspx.cs and add following lines:

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString());
    protected void Page_Load(object sender, EventArgs e)
    {
            DataSet ds = new DataSet();
            DataView dv = new DataView();
dv = binddata();
            GridView1.DataSource = dv;
            GridView1.DataBind();
    }
    private DataView binddata()
    {
        conn.Open();
  SqlDataAdapter adp = new SqlDataAdapter("select data from table", conn);
        adp.Fill(ds);
        if (ViewState["sortexp"] != null)
        {
            dv = new DataView(ds.Tables[0]);
            dv.Sort = (string)ViewState["sortexp"];
        }
        else
            dv = ds.Tables[0].DefaultView;
        conn.Close();
        return dv;

     }
    protected void paging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataSource = binddata();
        GridView1.DataBind();
    }
    protected void sorting(object sender, GridViewSortEventArgs e)
    {
        ViewState["sortexp"] = e.SortExpression;
        GridView1.DataSource = binddata();
        GridView1.DataBind();
    }
}

Step 7. Open properties of GridView and click on events, change PageIndexChanging to paging and Sorting to sorting.

Step 8. Build the project.
 

Login to add your contents and source code to this article
Article Extensions
Contents added by rahul rannot on Nov 08, 2010
post comment
     
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts