Gridview sorting:
//create your connection string
string conString = ConfigurationManager.ConnectionStrings["samplecon"].ConnectionString;
SqlConnection Con = null;
1.fill your gridview using datatable in 'fillgrid()' function
using (Con = new SqlConnection(conString))
{
Con.Open();
SqlCommand cmd = new SqlCommand("your stored procedure name", Con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
DataView mydataview = new DataView();
mydataview = dt.DefaultView;
if (!String.IsNullOrEmpty(Convert.ToString(ViewState["SortDirection"])) && !String.IsNullOrEmpty(Convert.ToString(ViewState["SortExpression"])))
{
mydataview.Sort = string.Format("{0} {1}", Convert.ToString(ViewState["SortExpression"]), Convert.ToString(ViewState["SortDirection"]));
}
GridView1.DataSource = mydataview;
GridView1.DataBind();
}
2. Sorting code used on gridview in Sort event
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
ViewState["SortExpression"] = e.SortExpression;
if ((Convert.ToString(ViewState["SortDirection"]).Equals("ASC")))
{
ViewState["SortDirection"] = "DESC";
}
else
{
ViewState["SortDirection"] = "ASC";
}
fillgrid();
}
Gridview Paging:
1. Paging code used on gridview in PageIndexChanging event
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
fillgrid();
}
2. Gridview pagesize:
example:
select your gridview properties
pagesize=10