add a datasource column to a listbox help...

T

trint

I want to bind just the first column from a sql query to a list box so
that it only contains the first column results. Like:
listBox1_______
101
201
301
401____________

I already have the data bound (but it's the whole row) to a
datagridview:
DataTable table0 = new DataTable();
table0.Locale =
System.Globalization.CultureInfo.InvariantCulture;
dataAdapter0.Fill(table0);
bindingSource8.DataSource = table0;
dataGridView8.DataSource = bindingSource8;

Can I use that somehow to get my first column? e.g.
(listBox2.DataSource = table0["id"].DefaultView;
I know the above on the listbox doesn't work, but something like that?

Any help is appreciated.
Thanks,
Trint
 
N

Nicholas Paldino [.NET/C# MVP]

Trint,

In order to be connected to the same set of data, in the same context,
you need to be bound to the same data, exactly. In this case, this means be
bound in the same context, you have to set the ListBox to the same data
source. In this case, that means binding to "bindingSource8". Now, in
order to get the first column to be displayed in the listbox, you have to
set the DisplayMember property to the name of that column. You can get this
from table0 (specifically, the columns collection).
 
T

trint

Trint,

In order to be connected to the same set of data, in the same context,
you need to be bound to the same data, exactly. In this case, this means be
bound in the same context, you have to set the ListBox to the same data
source. In this case, that means binding to "bindingSource8". Now, in
order to get the first column to be displayed in the listbox, you have to
set the DisplayMember property to the name of that column. You can get this
from table0 (specifically, the columns collection).

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)




I want to bind just the first column from a sql query to a list box so
that it only contains the first column results. Like:
listBox1_______
101
201
301
401____________
I already have the data bound (but it's the whole row) to a
datagridview:
DataTable table0 = new DataTable();
table0.Locale =
System.Globalization.CultureInfo.InvariantCulture;
dataAdapter0.Fill(table0);
bindingSource8.DataSource = table0;
dataGridView8.DataSource = bindingSource8;
Can I use that somehow to get my first column? e.g.
(listBox2.DataSource = table0["id"].DefaultView;
I know the above on the listbox doesn't work, but something like that?
Any help is appreciated.
Thanks,
Trint- Hide quoted text -

- Show quoted text -

Thanks...Here is the code that worked:
listBox2.DataSource = bindingSource8;
listBox2.DisplayMember = "id";
Thanks,
Trint
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top