set the value of a combobox that is bound to a typedList

Posts   
 
    
yogiberr
User
Posts: 432
Joined: 29-Jun-2005
# Posted on: 17-Apr-2006 16:32:51   

version 1.0.2005.1 final (self-servicing) VS2005 winforms


Hiya, I have a combobox that is bound to a typedList.

TListRunTypedList currRuns = new TListRunTypedList();
            currRuns.Fill();

cboRun.Valuemember = "runId"
cboRun.Displaymember = "runName"
cboRun.DataSource = currRuns;

I want to set the selectedValue of the combobox, so that it selects the correct item.

However, when I try below, the combobox appears completely empty.

cboRun.SelectedValue = _selectedDeliveryId;

Hmm, can anyone show me what I'm doing wrong?

many thanks,

yogi

JimHugh
User
Posts: 191
Joined: 16-Nov-2005
# Posted on: 17-Apr-2006 22:47:51   

Does the comboxbox have anything bound to the SelectedValue?

Code something like:


cboRun.DataBindings.Add(New System.Windows.Forms.Binding("SelectedValue", this.BindingSource1, "RunId", True));

yogiberr
User
Posts: 432
Joined: 29-Jun-2005
# Posted on: 18-Apr-2006 00:57:45   

hiya Jim,

Does the comboxbox have anything bound to the SelectedValue?

the only bindings to the combobox were the one that I listed.

I took your advice and:

When I add this code, then I get the error: this causes two bindings in the collection to bind to the same property.

hmm, so I do the following:


cboRun.DataBindings.Clear(); 
 cboRun.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", currRuns, "runId", true));

Now, I don't get the error, but the combobox doesn't get populated, at all.

If I try:

cboRun.DataBindings.Add(new System.Windows.Forms.Binding("Text", currRuns, "runId", true));

Then the combobox is poplated with the value "0"

At the moment, I've no idea why: 1) my original code only poplates the combobox, it doesn't allow me to "set" the selected item 2) the code that you posted doesn't populate the combobox

does this shed any light?

I'm struggling here.

cheers,

yogi

JimHugh
User
Posts: 191
Joined: 16-Nov-2005
# Posted on: 18-Apr-2006 01:30:28   

If you had to clear the databindings before adding one, it appears that you speciifed one in the property grid for the combobox. The databinding code would then have been present in the formX.designer.cs file.

The DataBinding and the DataSource should generally be from two different tables/entity collections.

The SelectedValue should be a FK to a PK in another table.

It seems we have been down this same road before simple_smile

Step back a moment and explain more about what you are trying to do in this case.

yogiberr
User
Posts: 432
Joined: 29-Jun-2005
# Posted on: 18-Apr-2006 02:20:31   

you're right Jim,

You see, I wanted to create a "read only" combobox.

I was trying to take a shortcut by simply: 1) binding the combobox to a typedList 2) "forcing" a selection in the combobox, by setting the selectedValue.

Obviously, that isn't working.So, it's down the databindings collection route I will go. Forget about "runs", the easiest way for me to explain is by talking about delivery drivers.

I have a winform that displays all the products in a delivery in a datagridView Above this datagridview there are a few comboxes...one of which represents the particular a list of delivery drivers. Obviously, each delivery has a specific driver.

So, I know that I should ditch the tyedLists, in favour of the databindings collection.


tblDeliveryCollection1.GetMulti(null);
            driverBindingSource.DataSource = tblDeliveryCollection1;    
            cboDrivers.DataBindings.Clear();
            cboDrivers.DataSource = driverBindingSource;
        
cboDrivers.DisplayMember = dalProject.TblDriverFieldIndex.DriverName.ToString();        
cboDrivers.ValueMember = dalProject.TblDriverFieldIndex.DriverId.ToString();
cboDrivers.DataBindings.Add(new Binding("SelectedValue", this.driverBindingSource, TblDriverFieldIndex.DriverId.ToString(), true));

hmm, I think that this is nearly right..but at the moment, cboDriver displays several drivers MULTIPLE times.

have I made an easy mistake?

many thanks,

yogi

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 18-Apr-2006 15:56:58   

I want to set the selectedValue of the combobox, so that it selects the correct item. However, when I try below, the combobox appears completely empty. cboRun.SelectedValue = _selectedDeliveryId; Hmm, can anyone show me what I'm doing wrong?

Would you please try the following?

cboRun.SelectedIndex = cboRun.Items.IndexOf(cboRun.Items.FindByValue(YourValue));
yogiberr
User
Posts: 432
Joined: 29-Jun-2005
# Posted on: 19-Apr-2006 01:17:59   

hiya Walaa,

I think it's only the "System.Web.UI.WebControls.ListItemCollection" that supports "FindByValue", I'm using winforms.

Jim correctly stated that he had previously helped me to solve a similar problem using the databindings.

However, this is slightly different, because: 1) I am binding the datagridview to a "child" collection 2) I also need the parentCollection, because I am not using in-line datagridView editing.

It's all a bit messy, so I'll get my explanation clear and re-post.

cheers, yogi