单击标题时,GridView未排序

时间:2019-04-11 07:05:02

标签: mysql asp.net

我创建了一个网格视图,在其中添加了排序功能。 Gridview工作正常,但单击标题会出现此错误。 “对象引用未设置为对象的实例。”

我看过类似的帖子,但不明白原因。

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
    String mycon = "Data Source=localhost; Initial Catalog=sales_purchase; User Id=root;password=''";
    String myquery = "Select * from brand";
    MySqlConnection con = new MySqlConnection(mycon);
    MySqlCommand cmd = new MySqlCommand();
    cmd.CommandText = myquery;
    cmd.Connection = con;
    MySqlDataAdapter da = new MySqlDataAdapter();
    da.SelectCommand = cmd;
    DataSet ds = new DataSet();
    da.Fill(ds);
    con.Close();
    DataTable dt = ds.Tables[0];
    DataView dv = new DataView(dt);
    if (ViewState["sorting"].ToString() == "DESC" | ViewState["sorting"] == null) //This is where error is displaying

        {
        dv.Sort = e.SortExpression + " ASC";
        ViewState["sorting"] = "ASC";
        Label1.Text = "List in Ascending Order";
    }
    else if (ViewState["sorting"].ToString() == "ASC")
    {

        dv.Sort = e.SortExpression + " DESC";
        ViewState["sorting"] = "DESC";
        Label1.Text = "List in Descending Order";
    }

0 个答案:

没有答案