Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Datagridview Exception: column does not allow null
 

Datagridview Exception: column does not allow null

I have a datagridview(dgv) control which is bound to a typed dataset.

There is a hidden column called "ItemId", it is a not null column in database, this is used when update the data in dgv.

Now, when the application is start and I create a new application, the dgv is empty and user need to type in data in the dgv.

However, when I finish editing a row, and try to exit out from the row, I got an Exception "Column ItemId does not allow null".

What should I do to allow the dgv to ignore the null ItemId?

Thank you in advance.

Terrence Chan  Wednesday, May 17, 2006 2:58 AM

Hi Alan,

Thanks for the reply.

I need the ID when user update the DGV, I need this to tell the database which data to update.

I take your suggestion which set the DGV to Virtual Mode and set the CellValueNeeded like this, and it works!

private void datagridview1_CellValueNeeded(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)

{

if (e.ColumnIndex == 2) //Description

{

this.datagridview1.Rows[e.RowIndex].Cells[0].Value = e.RowIndex; //put adummy value in Column 0(ID)

}

}

Terrence Chan  Wednesday, May 24, 2006 1:47 AM

Hi Terrence,

I don't think it's possible to by-pass it unless you've lossen the constriant.

For your case, I think you should set the identity column incremental seed and value to -1. So that it'll get the value when inserted into the database. Moreover, I think you can use the VirtualMode of DGV and the CellValueNeeded event to fill up the item id.

cwlaualex  Tuesday, May 23, 2006 2:57 AM

Hi Alan,

Thanks for the reply.

I need the ID when user update the DGV, I need this to tell the database which data to update.

I take your suggestion which set the DGV to Virtual Mode and set the CellValueNeeded like this, and it works!

private void datagridview1_CellValueNeeded(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)

{

if (e.ColumnIndex == 2) //Description

{

this.datagridview1.Rows[e.RowIndex].Cells[0].Value = e.RowIndex; //put adummy value in Column 0(ID)

}

}

Terrence Chan  Wednesday, May 24, 2006 1:47 AM
Although it's a bit off topic, I found that you're using the index directly to locate the "Description" column. Why don't you use the name instead? I believe this make the program easier to maintain. Unless you're very serious about performance.
cwlaualex  Wednesday, May 24, 2006 1:54 AM

You can use google to search for other answers

Custom Search

More Threads

• How Do I Format a DataGridView Column to datagridviewcheckbox Type.?
• Problem with Master/Details datagridview and combobox
• Typed Dataset Connection Already in XSD???
• Connecting to a Remote SQL Server via windows forms
• How could I modify ConnectionString at rutime?
• pRINT PROBLEM IN AJAX
• How to specify columns
• BindingNavigator error on last row delete
• Allow users to edit a row
• Bind to ComboBox