469,304 Members | 2,192 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,304 developers. It's quick & easy.

Bindingsource with Listbox - selection of new items in listbox

61
I have a listbox on my form which is bounded to a bindingsource.
The collection is of type List<T>.

If I add new elements to the bindingsource, I would like to select them in the listbox.
A sample like the following is working...but it is not working when the list contains an object.

int cnt = listBox2.Items.Count;
for (int i = cnt; i < (cnt + 10); i++)
{
bindingSource2.List.Add(i.ToString());
}

for(int i = cnt; i < (cnt+10); i++)
{
listBox2.SetSelected(i, true);
}

Any hints on how to select the added items in listbox!
The selectionmode of this listbox uses MultiExtended property.
Nov 8 '08 #1
2 12048
debasisdas
8,127 Expert 4TB
question moved to .NET forum.

Regards
Debasis
Nov 9 '08 #2
mldisibio
190 Expert 100+
Not seeing all the binding code, I will have to make some assumptions, but I can understand what might keep your code from working as desired, and I have created an example to make it work as desired.

My first is assumption is how you create the binding source, so let's assume this:
Expand|Select|Wrap|Line Numbers
  1.       List<string> list01 = new List<string>();
  2.       list01.Add("AAA");
  3.       list01.Add("BBB");
  4.       list01.Add("CCC");
  5.       Binding Source bindingSource2 = new BindingSource(list01, "");
  6.       this.listBox1.DataSource = bindingSource2;
  7.  
As such, your list box would display the items correctly, because by default the list box calls the "ToString()" method on the objects it is bound to in order to figure out what to display.

Your code to add some items dynamically via the BindingSource will work fine ASSUMING you have added this line, which I have not seen, AFTER adding and BEFORE trying to select the new items:
Expand|Select|Wrap|Line Numbers
  1. this.bindingSource2.ResetBindings(false);
  2.  
So far so good. So I create a dummy object and change the source list to List<MyThing>:
Expand|Select|Wrap|Line Numbers
  1.   public class MyThing {
  2.     string s;
  3.     int i;
  4.     public MyThing(string s, int i) {
  5.       this.s = s;
  6.       this.i = i;
  7.     }
  8.     public string SomeString { get { return this.s; } }
  9.     public int SomeInt { get { return this.i; } }
  10.   }
  11.  
So to add some initial objects it will look like:
Expand|Select|Wrap|Line Numbers
  1. list01.Add(new MyThing("X", 24));
  2. // etc
  3.  
And modifying your dynamic add/select code would look like:
Expand|Select|Wrap|Line Numbers
  1.       for (int i = cnt; i < (cnt + 10); i++) {
  2.         bindingSource2.List.Add(new MyThing(i.ToString(), i));
  3.       }
  4.  
As such, this will not display anything properly.
The "MyThing" object does not have a ToString() override.
The trick to get this to display properly is to add this line when you do your intial databinding:
Expand|Select|Wrap|Line Numbers
  1.       bindingSource2 = new BindingSource(list01, "");
  2.       this.listBox1.DisplayMember = "SomeString";
  3.       this.listBox1.DataSource = bindingSource2;
  4.  
Without posting all my sample code, which may have some minor difference from yours, this works. When I add the new MyThings, it displays the "SomeString" property of them, and selects the new ones I add.

By default a ListBox calls ToString() on the object it is bound to, unless you specify the DisplayMember property which is the property name of your bound object's property which you want to display. (The property of the items in the bound list, not a property of the list itself.)

Let me know if it still does not work or if you need clarification.

Mike
Nov 10 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by bh_jodokast | last post: by
3 posts views Thread by Mitch | last post: by
reply views Thread by GentOfTheLake | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.