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 = newSqlConnection(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.
No comments:
Post a Comment