Monday, May 4, 2009

Gridview Sorting

protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
FillGrid("CustomerID");
}
}

private void FillGrid(string col_name)
{
SqlConnection con = new SqlConnection("trusted_connection=yes;database=northwind;server=localhost");
SqlDataAdapter da = new SqlDataAdapter("select * from customers order by " + col_name, con);
DataSet ds = new DataSet();
da.Fill(ds, "customers");
//Sorted info is placed into Dataset ds.
GridView1.DataSource = ds.Tables["customers"];
GridView1.DataBind();
}

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
FillGrid(e.SortExpression);
//e.SortExpression will provide column selected by user.
}

No comments:

Post a Comment