473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 12693
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

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

Similar topics

4
by: KK | last post by:
Hi All... For my purpose, I need to handle listbox selection changed event.I must be notified before changing occurs and after.After I can handle using SelectedIndexChanged event.Is there anyway...
5
by: Lie | last post by:
Hi all, I have problem in getting selectedindex of multiple listbox selection in a datagrid. I have a listbox with multiple selection mode inside datagrid. In Edit mode, I need to get back all...
0
by: bh_jodokast | last post by:
It's pretty easy to do a drag from one listbox to another listbox when using Windows.Forms, but on the web, forms are handled differently. My plan is to use two listboxes and a DHTML HTC file to...
3
by: Mitch | last post by:
Is the following a correct representation of the relationship of the selected index to selected item: INDEX ITEM 0 "item 1" 1 "item 2" 2 "item 3" and so on. I keep...
1
by: anjupt | last post by:
Hi, I have 2 listboxes with data fetched from database so for both the listboxes datasource is set .i tried to move data from one listbox to another listbox but Error"An unhandled exception of...
0
by: GentOfTheLake | last post by:
Why won't the webcontrols version of the listbox work with this: DirectoryInfo di = new DirectoryInfo(); FileInfo fi = di.GetFiles(); ListBox lb = new ListBox(); lb.AddRange(fi); The forms...
5
by: Randy | last post by:
Hi, I'm have a form with a listbox and a few textboxes on it. Based on the user's selection in the listbox, I want the bindingsource to update the textboxes with the corresponding values. I have...
1
by: dkohel | last post by:
I have 2 list boxes on my form. I am trying to populate listbox B with the selection from listbox A. I have set multi-select in both boxes to Extended... The user will select the items from...
1
by: Gerardo ARnaez | last post by:
Hi. I am writing a program to help determine coumadin regimens to look at the code: http://sourceforge.net/projects/coumadinregimen/ The issue is that I have a variable that I want the use to...
5
by: sreenu123 | last post by:
Hi, I want to read from values from database and add to listbox then transfering items from one listbox to another listbox using C#.NET. Can any one give source code. Thanks in advance sree
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.