0
votes

I have a DataGridView which I have bound to a single ODBC database table using the Form Designer.

The ODBC source is an Informix database, and the relevant table contains an auto-incrementing column to generate the primary key for the table. There's another column which depends on a setting on the form. Both of these columns are set to Visible = False.

When I allow the "Enable Adding" DataGridView setting and add a new row to the DataGridView I get the error that the second column does not allow nulls (which is correct).

Is there any way I can allow users to add rows directly to the DataGridView, and then programmatically set this missing value on, for example, an event, or do I have to resort to having an "Add New Row" button which adds a new row to the DataSource?

1

1 Answers

4
votes

You can provide default values to the DataGridView using the DefaultValuesNeeded event.

The code will look something like this:

private void dataGridView1_DefaultValuesNeeded(object sender,
    System.Windows.Forms.DataGridViewRowEventArgs e)
{
    e.Row.Cells["Region"].Value = "WA";
    e.Row.Cells["City"].Value = "Redmond";
    e.Row.Cells["PostalCode"].Value = "98052-6399";
    e.Row.Cells["Country"].Value = "USA";
    e.Row.Cells["CustomerID"].Value = NewCustomerId();
}