locked
Foriegn key value in Datagridview RRS feed

  • Question

  • Hi there

    I have got a dataset with two tables ( parent child relationship). I have assigned the child table as a datasource on to one of the datagridview controls. When i try and insert a new row in the datagrid view control, its not picking up the foriegn key value from the parent table if that makes sense. I keep getting error messages saying "This column does not allow nulls" which is obvious. I know there is a way where i can grab the primary key from the parent table and assign it as a default value in the gridview but i would like to know if there is any other better way to do this. Any help on this would be highly appreciated!!

    Thanks very much in advance

    Monday, July 12, 2010 2:13 PM

Answers

  • Hi,

     

    Seems you still need to find the foreign key.

     

    By default, foreign key constraint can contain null value. But when pass value to insert command, it will check value type.

     

    You can use stored procedure to insert value. Pass the value to stored procedure, find the foreign key.

     

    Another workaround is use combobox column to keep the foreign ID. You can hide it if you don’t want it is visible.

     

    For example:

                DataGridViewComboBoxColumn comboboxcolumn1 = new DataGridViewComboBoxColumn();

                comboboxcolumn1.Name = "combobox";

                comboboxcolumn1.HeaderText = "ReferName";

                comboboxcolumn1.DataSource = dt01; // master datatable

                comboboxcolumn1.DisplayMember = "Name";

                comboboxcolumn1.ValueMember = "ID";

                comboboxcolumn1.DataPropertyName = "ReferID"; // foreign key column name in child datatable

     

    Hope this helps.

     

    Best regards,

    Ling Wang


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Proposed as answer by Paras Sanghani Friday, July 16, 2010 8:34 AM
    • Marked as answer by Jing0 Monday, July 19, 2010 2:23 AM
    Friday, July 16, 2010 8:30 AM

All replies

  • Hi,

     

    Seems you still need to find the foreign key.

     

    By default, foreign key constraint can contain null value. But when pass value to insert command, it will check value type.

     

    You can use stored procedure to insert value. Pass the value to stored procedure, find the foreign key.

     

    Another workaround is use combobox column to keep the foreign ID. You can hide it if you don’t want it is visible.

     

    For example:

                DataGridViewComboBoxColumn comboboxcolumn1 = new DataGridViewComboBoxColumn();

                comboboxcolumn1.Name = "combobox";

                comboboxcolumn1.HeaderText = "ReferName";

                comboboxcolumn1.DataSource = dt01; // master datatable

                comboboxcolumn1.DisplayMember = "Name";

                comboboxcolumn1.ValueMember = "ID";

                comboboxcolumn1.DataPropertyName = "ReferID"; // foreign key column name in child datatable

     

    Hope this helps.

     

    Best regards,

    Ling Wang


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Proposed as answer by Paras Sanghani Friday, July 16, 2010 8:34 AM
    • Marked as answer by Jing0 Monday, July 19, 2010 2:23 AM
    Friday, July 16, 2010 8:30 AM
  • Hi,

     

    If there is anything unclear, please feel free to ask.

     

    Best regards,

    Ling Wang


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Monday, July 19, 2010 2:23 AM