473,547 Members | 2,638 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Bindingsource with Listbox - selection of new items in listbox

61 New Member
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.ToSt ring());
}

for(int i = cnt; i < (cnt+10); i++)
{
listBox2.SetSel ected(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 12712
debasisdas
8,127 Recognized Expert Expert
question moved to .NET forum.

Regards
Debasis
Nov 9 '08 #2
mldisibio
190 Recognized Expert New Member
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
11832
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 to know which one is selected item before this event.?One thing is I can have a variable to hold the index in my application. I'm looking exactly...
5
3517
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 selected items of that listbox and display it. can anyone help? Thanks
0
1021
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 change the cursor (like if you drag out of bounds the cursor changes to "nodrag") My friend has a javascript snippet that does this too, but... I...
3
19065
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 getting the same string for selected index 0 and 1; i.e. both are "item 1". I'm new to C# and not exactly an accomplished programmer. Any suggestions?
1
2932
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 type 'System.ArgumentException' occurred in system.windows.forms.dll Additional information: Cannot modify the Items collection when the DataSource...
0
1044
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 version of the listbox handles this just fine. Does anyone know how to convert the FileInfo to ListItem for the
5
3583
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 similar code built for a combobox that works just fine, but when applying it to a listbox bound to a dataview, I can't figure out how to return the...
1
12841
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 listbox A and click an Add button that will then copy the items to listbox B. Then for each item in B, I need to add them to a database. If there...
1
5153
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 select if they don't like the default. I have made this as a listbox widget The rest of the gui is just entry widgets
5
11018
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
7510
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7437
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7703
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7947
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6032
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5362
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3473
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1923
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
748
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.