DataBindings and SelectedValue

G

Guest

Hi,

I use VS2003 and C# with windows form.

I have one combobox bind to an Objsourcelist (CollectionBase) who contains
some ObjSource object with "Description" and "ID".
--------------------------------------------------------------------------
cmbFLSOURCE.DataSource = Objsourcelist;
cmbFLSOURCE.DisplayMember = "Description";
cmbFLSOURCE.ValueMember = "ID";
---------------------------------------------------------------------------
When I run the form, everything is display ok, so I can see objsource
description values. However, when I add binding to the dataset for the
SelectedValue using my dataset table "Expense.fl_source" which match the
valueMember "ID", it never display the corresponding value Description, and
display blank value. Others fields bind to the same dataset show the right
information.
----------------------------------------------------------------------------
Binding b;
b = cmbFLSOURCE.DataBindings.Add("SelectedValue", myDSexpense,
"Expense.fl_source");
-----------------------------------------------------------------------------
Is there anything missing?

Thanks in advance.

CB
 
F

Frans Bouma [C# MVP]

Carl said:
Hi,

I use VS2003 and C# with windows form.

I have one combobox bind to an Objsourcelist (CollectionBase) who contains
some ObjSource object with "Description" and "ID".
--------------------------------------------------------------------------
cmbFLSOURCE.DataSource = Objsourcelist;
cmbFLSOURCE.DisplayMember = "Description";
cmbFLSOURCE.ValueMember = "ID";
---------------------------------------------------------------------------
When I run the form, everything is display ok, so I can see objsource
description values. However, when I add binding to the dataset for the
SelectedValue using my dataset table "Expense.fl_source" which match the
valueMember "ID", it never display the corresponding value Description, and
display blank value. Others fields bind to the same dataset show the right
information.

Erm... you're binding 2 datasources to 1 control? (cmbFLSOURCE) ?

That's not going to work. In a form, you have a currencymanager. This
object controls all the bindings. So if you bind the same datasource to
different controls (dataset to grid and to a textbox) you'll see the
data synced between the 2 controls.

Frans

--
 
G

Guest

Hi,

Ok it makes sense. The only reason I try this, is to display let say "Open"
in the control when the value is 0 in the table, and "Close" when it is 1.

I know I can use the binding parse and format, but this will add another
issue when we edit the value, I need to see all textual choice in the combo
for all possible value.

But when I do select another value in combo 1, when binding is in effect,and
go to another field, combo 1 will always return to original value.

Carl
 
G

Guest

Frans,

Frans Bouma said:
Erm... you're binding 2 datasources to 1 control? (cmbFLSOURCE) ?

I guess I'm missing something ... I don't see where he's binding 2
datasources to 1 control. Care to elaborate?

~~Bonnie
 
F

Frans Bouma [C# MVP]

Bonnie said:
Frans,

:




I guess I'm missing something ... I don't see where he's binding 2
datasources to 1 control. Care to elaborate?

Wasn't he creating a simple and a complex binding to the same control
(cmbFLSOURCE) ? (I lost the original message)

Perhaps I misread the posting, (which is probably the case ;)).

Frans

--
 
F

Frans Bouma [C# MVP]

Carl said:
Hi,

Ok it makes sense. The only reason I try this, is to display let say "Open"
in the control when the value is 0 in the table, and "Close" when it is 1.

I know I can use the binding parse and format, but this will add another
issue when we edit the value, I need to see all textual choice in the combo
for all possible value.

But when I do select another value in combo 1, when binding is in effect,and
go to another field, combo 1 will always return to original value.

Let's say dataset Foo has table "Bar" which has a column "Action".

If I bind bar to a grid:
myGrid.DataSource = Foo.Tables["Bar"];

then I'll see this table in the grid. If I then want to have a combo
box reflecting teh value of
Foo.Tables["Bar"].Rows[currentrow]["Action"], I'll do:
cmbFLSOURCE.DataSource = Foo.Tables["Bar"]
cmbFLSOURCE.DataMember = "Action";

(If I'm not mistaken).

then, traversing the grid's rows, will reflect the action column's
values in the combobox cmbFLSOURCE.

Frans


--
 
G

Guest

Frans Bouma said:
Wasn't he creating a simple and a complex binding to the same control
(cmbFLSOURCE) ? (I lost the original message)

Frans,

Yes, he had both simple and complex binding to his ComboBox. But, that's
what you're supposed to do with a ComboBox. The Combo's DataSource, the
"complex" binding, merely states from which DataTable you are populating the
Combo. The DataBinding to the .SelectedValue property, the "simple" binding,
works like any other simple databinding (say to a TextBox's .Text property).

As I mentioned to him in a post on another forum, referring to complex
binding as "databinding" can be very confusing to some people and I try to
avoid using that term.

~~Bonnie
 
G

Guest

(If I'm not mistaken). <<

Sorry, Frans, you *are* mistaken.

Using your example, if I want to have a combo box reflecting the value of
Foo.Tables["Bar"].Rows[currentrow]["Action"], I would do this:

cmbFLSOURCE.DataBindings.Add("SelectedValue", Foo.Tables["Bar"], "Action");

~~Bonnie
 
G

Guest

Also Bonnie suggest to use a datatable instead of my collection for the
datasource.

I try this and it works like a charm.

I will try to find out why it is not working with collection, but for the
moment, the datatable fix this issue.

Thanks to both of you,
 
F

Frans Bouma [C# MVP]

Bonnie said:
Sorry, Frans, you *are* mistaken.

Using your example, if I want to have a combo box reflecting the value of
Foo.Tables["Bar"].Rows[currentrow]["Action"], I would do this:

cmbFLSOURCE.DataBindings.Add("SelectedValue", Foo.Tables["Bar"], "Action");

Ermm.. and why not using DataSource and DisplayMember ? The currency
manager keeps it all in sync.

FB
Frans Bouma said:
Let's say dataset Foo has table "Bar" which has a column "Action".

If I bind bar to a grid:
myGrid.DataSource = Foo.Tables["Bar"];

then I'll see this table in the grid. If I then want to have a combo
box reflecting teh value of
Foo.Tables["Bar"].Rows[currentrow]["Action"], I'll do:
cmbFLSOURCE.DataSource = Foo.Tables["Bar"]
cmbFLSOURCE.DataMember = "Action";

(If I'm not mistaken).

then, traversing the grid's rows, will reflect the action column's
values in the combobox cmbFLSOURCE.
 
F

Frans Bouma [C# MVP]

Bonnie said:
Yes, he had both simple and complex binding to his ComboBox. But, that's
what you're supposed to do with a ComboBox. The Combo's DataSource, the
"complex" binding, merely states from which DataTable you are populating the
Combo. The DataBinding to the .SelectedValue property, the "simple" binding,
works like any other simple databinding (say to a TextBox's .Text property).

That's new to me that you have to set up 2 bindings for 1 control to
get it right. I always use 'DisplayMember' and that works as a charm.
The thing is: the combobox' items are the datarow elements of the bound
datatable. In your case perhaps they're not, I don't know. What I find
really strange is that you setup 2 bindings with 1 control, I never
heard of that before.
As I mentioned to him in a post on another forum, referring to complex
binding as "databinding" can be very confusing to some people and I try to
avoid using that term.

It's called complex databinding so that's the term he has to learn. The
reason that there is complex and simple databinding is because these are
completely different. Referring to both of them as 'databinding' is IMHO
more confusing.

Frans

--
 
G

Guest

Ermm.. and why not using DataSource and DisplayMember ? The currency
manager keeps it all in sync.<<

CurrencyManager for which table? I think you are still confusing complex and
simple binding. The DataSource, DisplayMember and ValueMember are strictly
for populating the ComboBox (complex binding). This is not the same table as
the table that gets set to the result of the user Selecting a value from the
Combo. For that, you need to use simple databinding and bind to the
..SelectedValue property of the Combo.

I assume that perhaps you are taking the .SelectedValue and programmatically
sticking it in a column of another DataTable. This is *not* the way to do it.

~~Bonnie



Frans Bouma said:
Bonnie said:
(If I'm not mistaken). <<
Sorry, Frans, you *are* mistaken.

Using your example, if I want to have a combo box reflecting the value of
Foo.Tables["Bar"].Rows[currentrow]["Action"], I would do this:

cmbFLSOURCE.DataBindings.Add("SelectedValue", Foo.Tables["Bar"], "Action");

Ermm.. and why not using DataSource and DisplayMember ? The currency
manager keeps it all in sync.

FB
Frans Bouma said:
Let's say dataset Foo has table "Bar" which has a column "Action".

If I bind bar to a grid:
myGrid.DataSource = Foo.Tables["Bar"];

then I'll see this table in the grid. If I then want to have a combo
box reflecting teh value of
Foo.Tables["Bar"].Rows[currentrow]["Action"], I'll do:
cmbFLSOURCE.DataSource = Foo.Tables["Bar"]
cmbFLSOURCE.DataMember = "Action";

(If I'm not mistaken).

then, traversing the grid's rows, will reflect the action column's
values in the combobox cmbFLSOURCE.
 
G

Guest

What I find
really strange is that you setup 2 bindings with 1 control, I never
heard of that before.

Well, with Combos (and ListBoxes), that's the way it is. I'm still confused
as to how you're using a ComboBox without doing this.

~~Bonnie
 

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