日期:2014-05-17 浏览次数:20491 次
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) { string sPage = e.SortExpression; if (ViewState["SortOrder"].ToString() == sPage) { if (ViewState["OrderDire"].ToString() == "Desc") ViewState["OrderDire"] = "ASC"; else ViewState["OrderDire"] = "Desc"; } else { ViewState["SortOrder"] = e.SortExpression; } BindGridView(); }
protected void Page_Load(object sender, EventArgs e) { if (ViewState["ddl"] != null) { DropDownList1.SelectedValue = ViewState["ddl"].ToString(); } if (!Page.IsPostBack) { ViewState["SortOrder"] = "ProductId"; ViewState["OrderDire"] = "ASC"; ViewState["sql"] = sql; BindGridView(); } }
public void BindGridView() { sql = ViewState["sql"].ToString(); SqlConnection con = new SqlConnection(str); try { con.Open(); SqlDataAdapter ad = new SqlDataAdapter(sql, con); DataSet ds = new DataSet(); ad.Fill(ds, "Product"); DataView dv = ds.Tables["Product"].DefaultView; string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"]; dv.Sort = sort; GridView1.DataSource = dv; GridView1.DataBind(); } catch (Exception e) { Page.RegisterStartupScript("show", "<script>alert('Error" + e.Message + "')</script>"); } finally { con.Close(); } }