Binding a ListBox to a DataTable

G

Guest

I'm trying to bind my listbox to a SQL query like this:

using (SqlConnection conn = new SqlConnection(CONN_STR)) {
using (SqlDataAdapter da = new SqlDataAdapter("roboticSiteNames", conn)) {
DataSet ds = new DataSet();

da.Fill(ds);

DataTable dt = ds.Tables[0];

lbSiteCode.DataSource = dt;
lbSiteCode.DisplayMember = dt.Columns[0].ColumnName;
lbSiteCode.ValueMember = dt.Columns[1].ColumnName;
}
}

Column 0 is a varchar(10), and column 1 is a tinyint. I see the names
properly in the listbox, but when I get into my event handler to see which
item was selected, and I do this: lbSiteCode.SelectedValue.ToString()

I get back a DataRowView instead of the expected tinyint value. What am I
doing wrong?
 
Y

Yura

Scott said:
I'm trying to bind my listbox to a SQL query like this:

using (SqlConnection conn = new SqlConnection(CONN_STR)) {
using (SqlDataAdapter da = new SqlDataAdapter("roboticSiteNames", conn)) {
DataSet ds = new DataSet();

da.Fill(ds);

DataTable dt = ds.Tables[0];

lbSiteCode.DataSource = dt;
lbSiteCode.DisplayMember = dt.Columns[0].ColumnName;
lbSiteCode.ValueMember = dt.Columns[1].ColumnName;
}
}

Column 0 is a varchar(10), and column 1 is a tinyint. I see the names
properly in the listbox, but when I get into my event handler to see which
item was selected, and I do this: lbSiteCode.SelectedValue.ToString()

I get back a DataRowView instead of the expected tinyint value. What am I
doing wrong?

Try to set DisplayMember and Value member before DataSource:

using (SqlConnection conn = new SqlConnection(CONN_STR))
using (SqlDataAdapter da = new sqlDataAdapter("roboticSiteNames", conn)) {
DataSet ds = new DataSet();
da.Fill(ds);
DataTable dt = ds.Tables[0];

// 1. set DisplayMember and ValueMember
lbSiteCode.DisplayMember = dt.Columns[0].ColumnName;
lbSiteCode.ValueMember = dt.Columns[1].ColumnName;
// 2. set DataSource
lbSiteCode.DataSource = dt;
}
 
G

Guest

Yura said:
Try to set DisplayMember and Value member before DataSource:

That let's me get the SelectedValue properly, but the SelectedItem doesn't
come back in code properly still.

Is it possible to re-bind? I have a second listbox that changes based on
the first listbox's selection. My Debug.Print() shows that the table coming
back has the new, changed values, but the ListBox doesn't update when I
reassign the DataSource on the second ListBox.
 
Y

Yura Korolev

Scott said:
That let's me get the SelectedValue properly, but the SelectedItem doesn't
come back in code properly still.

SelectedItem always returns current DataRow/DataRowView or object (if you
bind collection of objects) not SelectedValue
Is it possible to re-bind? I have a second listbox that changes based on
the first listbox's selection. My Debug.Print() shows that the table
coming
back has the new, changed values, but the ListBox doesn't update when I
reassign the DataSource on the second ListBox.

Do not create new DataTable, just use the same.

using (SqlConnection conn = new SqlConnection(CONN_STR))
using (SqlDataAdapter da = new sqlDataAdapter("roboticSiteNames", conn)) {

DataTable dt = lbSiteCode.DataSource as DataTable;
if (dt == null) {

dt = new DataTable();

da.Fill(dt); // fill the table

lbSiteCode.DisplayMember = dt.Columns[0].ColumnName;
lbSiteCode.ValueMember = dt.Columns[1].ColumnName;

lbSiteCode.DataSource = dt;
}
else
da.Fill(dt);
}
 

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