Insert ForeignKey of parent DataGrid in child

S

Sebastian-Hiller

Hi,

I have two dataGrids bound to two different datasets, which relate to
each other as parent/child. The primary key/foreign key columns are
hidden in the DataGrid. If I select a Parent the corresponding
child's are displayed.
I want to add a row to the child, with the currently selected parents
primary key as foreign key. How can I insert the right foreign key? I
tried the DataTable "RowChanging" and "RowChanged" event, but I got a
NOT-NULL violation from the DataGrid. I tried to use a DefaultValue and
then changed this DefaultValue within the RowChanging-Event, that
caused an error ("Can't change default value in RowChanging
event.").

I miss a "NewRow" event in DataGrid or something like that, so that I
can simply edit the new row and insert the proper foreign key.

My current solution is to catch the OnMouseUp Event on Parent DataGrid
and set child Foreign key column Default Value to the selected parent
Primary Key value. But this is more a hack then a Solution:

private void dataGridBenchmark_MouseUp(object sender,
System.Windows.Forms.MouseEventArgs e)
{
dataSetBenchmarks1.BenchmarkVersion.Benchmarkprogramm_IDColumn.DefaultValue=dataSetBenchmarks1.Benchmarkprogramm[dataGridBenchmark.CurrentRowIndex].Benchmarkprogramm_ID;
}

The problem is, that the user has to click the parent and can't use
the keyboard.

Any ideas? Thanks!
 
S

Sebastian-Hiller

Hi again,

turns out I did a mistake in my code. I assigned the wrong column in a
relationship. After I corrected the relationship every thing runs
smooth, the foreign keys in the child DataGrid are automatically set to
the selected Parent Primary Key, then inserting a new row.

I have to apologize.

Best regards.
Sebastian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top