Gridview check if its empty or null
hello I have a code that pulls the data to the gridview using a dataset, what is the best way to check if the Gridview is empty and if it is to not throw an error.. Right now my gridview has the setting to show a message if its empty.. but I just want to null and empty check after attempting to get the data in the dataset
Students students = new Students();
DataSet studentsList = students.GetAllStudents();
GridView1.Dat开发者_如何学运维aSource = studentsList;
GridView1.DataBind();
If I understand your question correctly, why not just check if the DataSet is empty, before you bind it to the GridView?
If it is, just don't bind it.
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();
}
You can count the returned row if its has data or not:
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!";
}
Regards
精彩评论