Foreign Key columns not always filled up when adding new rows in a datagridview bind to a BindingSource  
Author Message
Sven De Bont





PostPosted: Windows Forms Data Controls and Databinding, Foreign Key columns not always filled up when adding new rows in a datagridview bind to a BindingSource Top

Hi all,

I have a dataset, containing 3 tables:

ParentTable (Primary Key = ParentID)
ChildTable1 (Primary Key = Child1ID
ChildTable2 (Primary key = Child2ID

A relation (named Relation1) exists between ParentTable and ChildTable1
A relation (named Relation2) exist between ParentTable and ChildTable2

I have a form containing 3 Bindingsource instances:

BindingSource1 (DataSource = dataSet, DataMember = ParentTable)
BindingSource2 (DataSource = BindingSource1, DataMember = "Relation1")
BindingSource3 (DataSource = BindingSource1, DataMember = "Relation2")

The forms contains several textbox controls, all of them bound to BindingSource1.

The form contains 2 DataGridView controls

DataGridView1 (DataSource = BindingSource2)
DataGridView2 (DataSource = BindingSource3)

I have overriden the AddingNew event of all the BindingSources in order to add some custom initialization when a row is added.

When I open an existing row from the ParentTable, all related rows from ChildTable1 and ChildTable2 (if any) are correctly displayed in the DataGridView control. I am able to change/add/remove rows from the GridView controls as expected.

When I add a new row to the ParentTable (by calling BindingSource1.AddNew), I get the following 'behavior'

When I enter some rows in DataGridView1 (ChildTable1) and then try to add some rows to DataGridView2, the foreign key column contains null values. And I get an error as soon as I stop editing the DataGridView (saying that the field can't be null)

When I fist enter some rows in DataGridView2 (ChildTable2) and then in DataGridView1 (ChildTable1) it all works perfectly!!

I tried to work around the problem set the value of the foreign key field myself when it was null, but this causes the new rows simply to dissapear from the DataGridView

Here's some sample code:

public class Form1 : Form {

private BindingSource BindingSource1;

private BindingSource BindingSource2;

private BindingSource BindingSource3;

public Form1(DataSet ds) {

InitializeComponent();

BindingSource1 = new BindingSource();

BindingSource2 = new BindingSource();

BindingSource3 = new BindingSource();

BindingSource1.DataSource = ds;

BindingSuorce2.DataMember = "ParentTable"

BindingSource2.DataSource = BindingSource1;

BindingSource2.DataMember = "Relation1";

BindingSource3.DataSource = BindingSource1;

BindingSource3.DataMember = "Relation2";

DataGridView1.DataSource = BindingSource2;

DataGridView2.DataSource = BindingSource3;

BindingSource1.AddingNew += new AddingNewEventHandler(BindingSource1_AddingNew);

BindingSource2.AddingNew += new AddingNewEventHandler(BindingSource2_AddingNew);

BindingSource3.AddingNew += new AddingNewEventHandler(BindingSource3_AddingNew);

}

void BindingSource1_AddingNew(object sender, AddingNewEventArgs e) {

//get the bindingsource

BindingSource bs = (BindingSource)sender;

//get the underlying dataview

System.Data.DataView view = (System.Data.DataView)bs.List;

//get a new row

DataRowView row = view.AddNew();

//do some initalization of the new row

//....

e.NewObject = row;

}

void BindingSource2_AddingNew(object sender, AddingNewEventArgs e) {

BindingSource1.EndEdit();

//get the bindingsource

BindingSource bs = (BindingSource)sender;

//get the underlying dataview

System.Data.DataView view = (System.Data.DataView)bs.List;

//get a new row

DataRowView row = view.AddNew();

//here, the foreing key has been correctly set to the id of the

//ParentTable

//do some initalization of the new row

//....

e.NewObject = row;

}

void BindingSource3_AddingNew(object sender, AddingNewEventArgs e) {

BindingSource1.EndEdit();

//get the bindingsource

BindingSource bs = (BindingSource)sender;

//get the underlying dataview

System.Data.DataView view = (System.Data.DataView)bs.List;

//get a new row

DataRowView row = view.AddNew();

//If records have been added to BindingSource2 (by adding rows

//to DataGridView1, the foreing key field of the row contains a DBNull value

//(The problem does not occur when editing an existing ParentTable row)

//do some initalization of the new row

//....

e.NewObject = row;

}

}

Any ideas will be greatlyl appreciated.

Regards,




Windows Forms6  
 
 
raayu





PostPosted: Windows Forms Data Controls and Databinding, Foreign Key columns not always filled up when adding new rows in a datagridview bind to a BindingSource Top

Were you able to solve the problem...I am a newbie looking to make my master and 2 detail grids editable...I have same kind of relationships...among the grids ...Display works fine just want to add controlled edit feature..new row at the click of button...Are you doing that too...
Just found that you are doing samething...
                           Also how are these addingnew events triggered...is allowuserstoaddrows property true or false.....