【问题标题】:Gridview check if its empty or nullGridview 检查它是否为空或 null
【发布时间】:2014-03-03 20:02:26
【问题描述】:

你好我有一个使用数据集将数据拉到gridview的代码,检查Gridview是否为空以及是否不抛出错误的最佳方法是什么。现在我的gridview设置为如果它为空则显示一条消息.. 但我只想在尝试获取数据集中的数据后进行空检查和空检查

 Students students = new Students();
    DataSet studentsList = students.GetAllStudents();
    GridView1.DataSource = studentsList;
    GridView1.DataBind();

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    如果我正确理解您的问题,为什么不直接检查if the DataSet is empty,然后再将其绑定到 GridView?

    如果是,就不要绑定它。

    DataSet studentsList = students.GetAllStudents();
    bool empty = IsEmpty(studentsList); // check DataSet here, see the link above
    if(empty)
    {
        GridView1.Visible = false;
    }
    else
    {
        GridView1.DataSource = studentsList;
        GridView1.DataBind();
    }
    

    【讨论】:

      【解决方案2】:

      你可以统计返回的行是否有数据:

       DataSet studentsList = students.GetAllStudents();
      
       if(studentList.Tables[0].Rows.Count > 0) //COUNT DATASET RECORDS
      {
          GridView1.DataSource = studentsList;
          GridView1.DataBind();
      }
      else
      {
         lblError.Text = "NO RECORDS FOUND!";
      }
      

      问候

      【讨论】:

        猜你喜欢
        • 2021-11-21
        • 1970-01-01
        • 1970-01-01
        • 2020-03-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-18
        • 1970-01-01
        相关资源
        最近更新 更多