blah blah blah is here! blah blah » Close

up0down
link

Hi all

I'm doing a winform appl. in visual studio 2008, and I would like to know if someone can please help me? I have a sql server database and it's easy to bind for example all names from the database to a listbox, but what if I want to select the names A-L in one listbox and the names M-Z in an other listbox. What would be the best approach for this matter, is it even possible to databind these listboxes (visually) and with a query select them?

Thank you all.

last answered 4 months ago

1 answers

up0down
link

I've no idea how you'd do this visually but, if you have a DataTable called 'table' which has a column called 'Name', then you could filter the table and place the results into different listboxes using the following code:

DataView view1 = new DataView(table);
DataView view2 = new DataView(table);
view1.RowFilter = "Name < 'M'";
view2.RowFilter = "Name >= 'M'";
listBox1.DataSource = view1;
listBox1.DisplayMember = "Name";
listBox2.DataSource = view2;
listBox2.DisplayMember = "Name";


EDIT

I think I've figured out how to do this from the VS designer.

When you add a DataSource to your ListBox and set the DisplayMember property, a BindingSource object will be created. Just click on that to bring up its properties in the Properties Box and set the Filter property to the appropriate expression.

Now create a different DataSource for your other ListBox even though its referencing the same DataTable as the first one and set the DisplayMember property. A new BindingSource object will be created and you can set its Filter property to a complimentary expression to the first one.

A bit messy but it seemed to work OK when I tried it.

Thank you Vulpes, great as usual...by visually I meant this: if you drag a listbox to your form, then there is a small arrow (on the top of the listbox) you can click and a menu appears. You can databind if you check and select valuemember displaymember etc...Thanks again.

vulpes
11603

Please see my edit.

Feedback