Visual Basic 2005: Windows Forms BindingSource

G

Guest

I have a Form using two DataTables:
Company: CompanyID*, CompanyName, ...
Department: CompanyID*, DeparmentID, DepartmentName, Address..

I Read the company table and put in in a ComboBox

In ComboBox_ValueChanged i Read the Departments for the selected Company
All the columns are displayed in TextBoxes that are databound via a
BindingSource.

I do a BindingSource.AddNew() and fill in the Textboxes.

the New datarow is not accepted into the datatable because
Department.CompanyID is NULL.

The companyID is not databound to a control becaue it has been used as a
select criteria when the table was fetched. Does anyone have a soultion on
how to assign a value
to an unbound column attached to a BindinmgSource?
 
B

Bart Mermuys

Hi,

Jörgen said:
I have a Form using two DataTables:
Company: CompanyID*, CompanyName, ...
Department: CompanyID*, DeparmentID, DepartmentName, Address..

I Read the company table and put in in a ComboBox

In ComboBox_ValueChanged i Read the Departments for the selected Company
All the columns are displayed in TextBoxes that are databound via a
BindingSource.

I do a BindingSource.AddNew() and fill in the Textboxes.

the New datarow is not accepted into the datatable because
Department.CompanyID is NULL.

The companyID is not databound to a control becaue it has been used as a
select criteria when the table was fetched. Does anyone have a soultion on
how to assign a value
to an unbound column attached to a BindinmgSource?

You can use the DataRowView returned from BindingSource.AddNew to set
unbound fields, eg:
DataRowView drv = (DataRowView)DepartmentBindingSource.AddNew();
drv["CompanyID"] = .... ;

Or in your case you can also add a relation between your DataTable's, then
you can use CompanyBindingSource as the DataSource for the
DepartmentBindingSource and the relation name as the DataMember. Then when
you use AddNew on the DepartmentBindingSource the foreign key will
automatically get the pk from the currently selected Company.

DataSet ds = new DataSet();

ds.Tables.Add(CompanyTable);
ds.Tables.Add(DepartmentTable);

ds.Relations.Add( "Company2Department",
CompanyTable.Columns["CompanyID"], _
DepartmentTable.Columns["CompanyID"] );

CompanyBindingSource.DataSource = CompanyTable;

DepartmentBindingSource.DataSource = CompanyBindingSource;
DepartmentBindingSource.DataMember = "Company2Department";

CompanyComboBox.DataSource = CompanyBindingSource;
CompanyComboBox.DisplayMember = "CompanyName";

TextBox1.DataBindings.Add ( "Text", DepartmentBindingSource,
"DepartmentName" );
// ...

HTH,
Greetings
 

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