Hi cgs1973,
We can add a Validating event handler to the DataGridView. In the handler we can check the values and set the ErrorText of the row if necessary. This is a code snippet:
void dataGridView1_Validating(object sender, CancelEventArgs e)
{
bool hasError = false;
foreach (DataGridViewRow row in this.dataGridView1.Rows)
{
object value = row.Cells[0].Value;
if (value == null)
{
row.ErrorText = "The value of the first column cannot be null";
hasError = true;
}
else
{
int isMandatory = 0;
bool parseSucess = int.TryParse(value.ToString(), out isMandatory);
if (parseSucess == false)
{
row.ErrorText = "The value of the first column is not an integer.";
hasError = true;
}
else
{
if (isMandatory != 0 && isMandatory != 1)
{
row.ErrorText = "The value of the first column must be 0 or 1.";
hasError = true;
}
else if (isMandatory == 1)
{
//Column 2 is mandatory, so it cannot be null.
object value2 = row.Cells[1].Value;
if (value2 == null || value2.ToString().Trim() == "")
{
row.ErrorText = "The value of the first column is 1, so the value of the second column cannot be null.";
hasError = true;
}
}
}
}
}
if (hasError)
e.Cancel = true;
}
Let me know if this helps or not.
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.