I am in need of a simple way for checking if my datagridview is/isn't empty.
The Rows.Count does not satisfy me , because my program starts with 2 empty rows,
and in the future the datagridview could be populated and then the count
does not affect anything
(If the users deletes a section but there are more than 2 rows present).
Is there anyway of checking this?
well these are the checking options for whether datagrid view is empty or not ......
if(DataGridView1.Rows.Count == 0)
{
MessageBox.Show("DataGridView is empty");
}
2). You can check the DataTable or DataSet which binds to DataGridView:
if(dt == null)
{
MessageBox.Show("DataGridView is empty");
}
if(ds == null)
{
MessageBox.Show("DataGridView is empty");
}
you can check with datagrid view cell value also by using this:
if (dataGridView1.Columns[e.ColumnIndex].Name == "companyName")
{
if (String.IsNullOrEmpty(e.FormattedValue.ToString()))
{
dataGridView1.Rows[e.RowIndex].ErrorText =
"company Name must not be empty";
e.Cancel = true;
}
}
dataGridView1 with enable Adding:
using System.Linq;
if (dataGridView1.Rows.OfType<DataGridViewRow>().Take(2).Count() > 1)
{
MessageBox.Show("dataGridView1 has at least 2 rows");
}
dataGridView1 with disable Adding:
if (dataGridView1.Rows.OfType<DataGridViewRow>().Any())
{
MessageBox.Show("dataGridView1 has row");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.