How would i go about checking if a row's cells have data in them, ie not empty/null.
I've been trying the following:
if (dgvClient.SelectedRows.Count > 0)
{
DataGridViewRow currentRow = dgvClient.SelectedRows[0];
if (currentRow.Cells.ToString() != String.Empty)
{
//The code that will be here will open a form
}
else
{
MessageBox.Show("Select a non null row");
}
}
However, it doesn't appear to be working, and I'm out of ideas :/
Thanks for any help, Ari
You can find out if it is empty by checking the number of rows in the DataGridView. If myDataGridView. Rows. Count == 0 then your DataGridView is empty.
Solution 1 Either you strip off the data binding and do the boilerplate code yourself. Or you add a new record to the datasource and refresh the gridview => it will update itself with the new row.
.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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With