binding a DataTable as Combobox-DataSource

KriZa

Member
Joined
Jun 22, 2007
Messages
21
Programming Experience
3-5
Hello,
creating DataTable with content:


VB.NET:
Dim janein As DataTable = New DataTable("janein")
janein.Columns.Add("ID", Type.GetType("System.Int32"))
janein.Columns.Add("janein", Type.GetType("System.String"))
janein.PrimaryKey = New DataColumn() {janein.Columns("ID")}
Dim janein0 As DataRow = janein.NewRow()
janein0("ID") = 0
janein0("janein") = "Nein"
janein.Rows.Add(janein0)
Dim janein1 As DataRow = janein.NewRow()
janein1("ID") = 1
janein1("janein") = "Ja"
janein.Rows.Add(janein1)


table: janein
ID | janein
-----------
0 | nein
1 | ja

now I bind this table to different comboboxes that then is just the content of the table 'janein' able

Combo 'AbPf':

VB.NET:
.AbPf.DataSource = janein
.AbPf.DisplayMember = "janein"
.AbPf.ValueMember = "ID"
.AbPf.DataBindings.Add(New Binding("SelectedValue", bs_daten_antragsteller, "Abtretung_Pfaendung"))

Combo 'Zwang':

VB.NET:
.Zwang.DataSource = janein
.Zwang.DisplayMember = "janein"
.Zwang.ValueMember = "ID"
.Zwang.DataBindings.Add(New Binding("SelectedValue", bs_daten_antragsteller, "Zwang"))

Problem: if I switch the value of one Combobox, all Comboboxes switch their values to that I selected. THERE ARE NO EVENTS ON THESE COMBOBOXES! So I assume, that the binding is also reverse. Thats amazing and it would mean to create a DataTable for each Combobox.
It doesn't make sense at all. the binding (to my mind) is just oneway - take the Value 'Zwang' out of the table 'daten_antragsteller'. this is your Valuemember of the table 'janein'. get the related DisplayMember an show that in the Combobox. on swiching the value, save the Valumember in the table...
where is the bug?

thx and greets

KriZa
 
Problem: if I switch the value of one Combobox, all Comboboxes switch their values to that I selected. THERE ARE NO EVENTS ON THESE COMBOBOXES! So I assume, that the binding is also reverse.

Actually, when you say:

comboXXX.DataSource = myDataTable

The combo investigates myDataTable and upon finding it to be a datatable, binds to myDataTable.DefaultView instead

DefaultView is the default provision of a DataView. Datatables have no notion of position within the data, but the DataView does. Given that all combos are using the DefaultView i.e. the same dataview, then they all point to the same position and scrolling one scrolls the others.

Make some DataView of your own, or use BindingSource, which also implements view functionality and maintains own position:

Dim xxxBS as New BindingSource
xxxBS.DataSource = janeinTable
xxxCOmbo.DataSource = xxxBS

Dim yyyBS as New BindingSource
yyyBS.DataSource = janeinTable
yyyCombo.DataSource = yyyBS
 
Back
Top