PC Review


Reply
Thread Tools Rate Thread

DataBindings and SelectedValue

 
 
=?Utf-8?B?Q2FybA==?=
Guest
Posts: n/a
 
      12th Feb 2005
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


 
Reply With Quote
 
 
 
 
Frans Bouma [C# MVP]
Guest
Posts: n/a
 
      13th Feb 2005
Carl wrote:
> 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?


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

--
------------------------------------------------------------------------
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
 
Reply With Quote
 
=?Utf-8?B?Q2FybA==?=
Guest
Posts: n/a
 
      13th Feb 2005
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

 
Reply With Quote
 
=?Utf-8?B?Qm9ubmllIEJlcmVudCBbQyMgTVZQXQ==?=
Guest
Posts: n/a
 
      13th Feb 2005
Frans,

"Frans Bouma [C# MVP]" wrote:

> 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


 
Reply With Quote
 
Frans Bouma [C# MVP]
Guest
Posts: n/a
 
      13th Feb 2005
Bonnie Berent [C# MVP] wrote:
> Frans,
>
> "Frans Bouma [C# MVP]" wrote:
>
>
>> 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?


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

--
------------------------------------------------------------------------
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
 
Reply With Quote
 
Frans Bouma [C# MVP]
Guest
Posts: n/a
 
      13th Feb 2005
Carl wrote:
> 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


--
------------------------------------------------------------------------
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
 
Reply With Quote
 
=?Utf-8?B?Qm9ubmllIEJlcmVudCBbQyMgTVZQXQ==?=
Guest
Posts: n/a
 
      13th Feb 2005
"Frans Bouma [C# MVP]" wrote:
> 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

 
Reply With Quote
 
=?Utf-8?B?Qm9ubmllIEJlcmVudCBbQyMgTVZQXQ==?=
Guest
Posts: n/a
 
      13th Feb 2005
>>> (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



"Frans Bouma [C# MVP]" wrote:
> 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
>
>
> --
> ------------------------------------------------------------------------
> Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
> My .NET blog: http://weblogs.asp.net/fbouma
> Microsoft MVP (C#)
> ------------------------------------------------------------------------
>

 
Reply With Quote
 
=?Utf-8?B?Q2FybA==?=
Guest
Posts: n/a
 
      13th Feb 2005
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,

"Bonnie Berent [C# MVP]" wrote:

> "Frans Bouma [C# MVP]" wrote:
> > 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
>

 
Reply With Quote
 
Frans Bouma [C# MVP]
Guest
Posts: n/a
 
      14th Feb 2005
Bonnie Berent [C# MVP] wrote:
>>>> (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 [C# MVP]" wrote:
>> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Label Databindings are reset upon edit databindings via smart tag =?Utf-8?B?UFc=?= Microsoft ASP .NET 0 24th Apr 2006 12:10 AM
DataBindings NetRacer Microsoft VB .NET 0 27th Apr 2005 09:00 AM
DataBindings and SelectedValue =?Utf-8?B?Q2FybA==?= Microsoft Dot NET Framework Forms 0 9th Feb 2005 04:11 PM
databindings Kyle Rowe Microsoft C# .NET 0 6th Feb 2005 09:24 PM
DataBindings Chuck Bowling Microsoft C# .NET 2 4th Nov 2004 05:07 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:08 PM.