gpt4 book ai didi

c# - Gridview 检查它是否为空或 null

转载 作者:行者123 更新时间:2023-11-30 22:36:22 25 4
gpt4 key购买 nike

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

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

最佳答案

如果我正确理解你的问题,为什么不检查 if the DataSet is empty , 在将它绑定(bind)到 GridView 之前?

如果是,就不要绑定(bind)它。

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();
}

关于c# - Gridview 检查它是否为空或 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7048125/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com