locked
Datagridview Exception: column does not allow null RRS feed

  • Question

  • 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.

     

     

     

    Wednesday, May 17, 2006 2:58 AM

Answers

  • 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 a dummy value in Column 0(ID)

    }

    }

    Wednesday, May 24, 2006 1:47 AM

All replies

  • 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.

    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 a dummy value in Column 0(ID)

    }

    }

    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.
    Wednesday, May 24, 2006 1:54 AM