GridView空值排序,哪位大手帮忙看看?
GridView点击列名排序,如果这一列有空值,要求不管是升序还是降序,含有空值的行始终排在最后。(实际情况是:如果按照升序排序,空值行是排在最前面的)
排序事件
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(); } }