How to check if a selected row in a datagridview is empty(has no item) C#
.Cells
is a collection of DataGridViewCell
objects.
You need to iterate through that collection & test each cell to see if it has a value...
if (currentRow.Cells.Count > 0)
{
bool rowIsEmpty = true;
foreach(DataGridViewCell cell in currentRow.Cells)
{
if(cell.Value != null)
{
rowIsEmpty = false;
break;
}
}
if(rowIsEmpty)
{
MessageBox.Show("Select a non null row");
}
else
{
//DoStuff
}
}
Another method to check if a new empty row is selected perhaps
if(dataGridView.CurrentRow.Index == dataGridView.Rows.Count -1)
{
//you selected a new row
}