Hi,
I have a c# datagridview and I need to do some validation for cells before insert and update.here is my code.My issue is CellValidating is not firing up.
Can anyone show me the issue with code?
private void dataGridView1_CellValidating(object sender,
DataGridViewCellValidatingEventArgs e)
{
if (dataGridView1.Columns[e.ColumnIndex].Name == "OrderNo")
{
if (String.IsNullOrEmpty(e.FormattedValue.ToString()))
{
dataGridView1.Rows[e.RowIndex].ErrorText =
"Order No must not be empty";
e.Cancel = true;
}
}
}
void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
dataGridView1.Rows[e.RowIndex].ErrorText = String.Empty;
}
Thanks
Datagridview cell validation not working?
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Resign Designs35 pointsPosted Mar 13, 2015, 8:08 PM EDT
Resign Designs35 pointsPosted Mar 14, 2015, 8:08 PM EDT
private void dataGridView1_CellValidating(object sender,
DataGridViewCellValidatingEventArgs e)
{
if (e.ColumnIndex == 0)
{
if (String.IsNullOrEmpty(e.FormattedValue.ToString()))
{
dataGridView1.Rows[e.RowIndex].ErrorText =
"Line No must not be empty";
e.Cancel = true;
}
else if (e.ColumnIndex == 0)
{
int i;
if (!int.TryParse(Convert.ToString(e.FormattedValue), out i))
{
dataGridView1.Rows[e.RowIndex].ErrorText =
"Only numeric values for Line No";
e.Cancel = true;
}
}
}
1 short reply
Vithal WadjeRank #19 · 47.2K pointsPosted Mar 13, 2015, 10:58 AM EDT