C#: How do I check if a DataGrid is empty?
I have a DataGrid which I want to check开发者_JS百科 whether empty or not. Is there a way to do this? Currently, I have an idea like below code.
if (grdMass.DataSource=="")
{
cmdRelease.Enabled = false;
}
else
{
cmdRelease.Enabled = true; ;
}
Can someone help me please? Thank you.
To check if something is empty you can check if the Items property is empty.
grdMass.Items.Count == 0
The only way to check if the grid view is empty is by checking the number of rows it has.
if(gvMyData.Rows.Count == 0)
// Empty
else
// Not Empty
Hope this helps ;)
If nothing has been bound to the DataGrid
the DataSource
property will be null
:
cmdRelease.Enabled = (grdMass.DataSource != null);
精彩评论