none
DataGridView - column does not allow null values (Prevent row being automatically removed)

    Question

  • Hi

    I have a databound DataGridView and use a button to control updates.

    The problem is that if a db Column does not allow a null value an error is thrown ("Column <ID> does not allow null values") and then the datagridview automatically removes the row.  This is very frustrating as the user has to reinput all the data.

    I've looked online for help but can't seem to find a reasonable solution.   Any ideas on how to prevent this from happening?  Ideally, I could handle this error in the same way I handle "logic" errors - set the error icon and leave the row as it is.

    I don't want to use default values as this isn't always possible.

    Help is gratefully received.

    Gordon
    Wednesday, July 29, 2009 4:48 PM

Answers

  • Hello GWILSON,

    I have a solution for this. You can handle the DataError event of DataGridView and check if the cell is empty, Then call e.Cancel = true to trap the focus from getting rid of that cell.

    void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
            {
                if (e.ColumnIndex == 1 && dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString() == "")
                {
                    if (MessageBox.Show("XXX cannot be empty! Press Ok to enter a word, Press Cancel to discard this row edit", "Error",
                        MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == DialogResult.OK)
                    {
                        e.Cancel = true;
                    }
                    else
                    {
                        dataGridView1.CancelEdit();
                    }
                }
            }
    In this example, the second column(index = 1) cannot be empty. So I popup a MessageBox to tell the user the information. If user choose Ok, he will force to valid value for that cell. If he choose cancel, then the row will be rollback, that is your original function.

    Sincerely,
    Kira Qian
    Please mark the replies as answers if they help and unmark if they don't.
    • Marked as answer by GWILSON1981 Friday, July 31, 2009 10:44 AM
    Friday, July 31, 2009 3:36 AM

All replies

  • Hello GWILSON,

    I have a solution for this. You can handle the DataError event of DataGridView and check if the cell is empty, Then call e.Cancel = true to trap the focus from getting rid of that cell.

    void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
            {
                if (e.ColumnIndex == 1 && dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString() == "")
                {
                    if (MessageBox.Show("XXX cannot be empty! Press Ok to enter a word, Press Cancel to discard this row edit", "Error",
                        MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == DialogResult.OK)
                    {
                        e.Cancel = true;
                    }
                    else
                    {
                        dataGridView1.CancelEdit();
                    }
                }
            }
    In this example, the second column(index = 1) cannot be empty. So I popup a MessageBox to tell the user the information. If user choose Ok, he will force to valid value for that cell. If he choose cancel, then the row will be rollback, that is your original function.

    Sincerely,
    Kira Qian
    Please mark the replies as answers if they help and unmark if they don't.
    • Marked as answer by GWILSON1981 Friday, July 31, 2009 10:44 AM
    Friday, July 31, 2009 3:36 AM
  • Hello GWILSON,

    I have a solution for this. You can handle the DataError event of DataGridView and check if the cell is empty, Then call e.Cancel = true to trap the focus from getting rid of that cell.

    void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
            {
                if (e.ColumnIndex == 1 && dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString() == "")
                {
                    if (MessageBox.Show("XXX cannot be empty! Press Ok to enter a word, Press Cancel to discard this row edit", "Error",
                        MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == DialogResult.OK)
                    {
                        e.Cancel = true;
                    }
                    else
                    {
                        dataGridView1.CancelEdit();
                    }
                }
            }
    In this example, the second column(index = 1) cannot be empty. So I popup a MessageBox to tell the user the information. If user choose Ok, he will force to valid value for that cell. If he choose cancel, then the row will be rollback, that is your original function.

    Sincerely,
    Kira Qian
    Please mark the replies as answers if they help and unmark if they don't.

    Thank you very much, this is exactly what I was looking for.  Turns out I was putting "e.cancel=true" into the wrong event.
    Friday, July 31, 2009 10:44 AM