Skip to content

Datagridview cell validation not working?

RD Design

Asked Mar 13, 2015, 1:47 AM EDT3 answers
Share
LinkedInX
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

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • +1
    I need to check null, string and numeric validations.Please note this gridview accept user input and as soon as user completed the row editing it can save to db
    +1
  • +1
    I got this to work.Here is the code if someone else need to know how I solved it

    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
  • 1 short reply
    • Vithal Wadje
      +1
      what validation you wants ?
      +1