By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,705 Members | 1,301 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,705 IT Pros & Developers. It's quick & easy.

Binding a ListBox to a DataTable

P: n/a
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?

Nov 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Scott wrote:
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;
}

Nov 16 '05 #2

P: n/a


"Yura" wrote:
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.

Nov 16 '05 #3

P: n/a

"Scott" <Sc***@discussions.microsoft.com> wrote in message
news:71**********************************@microsof t.com...


"Yura" wrote:
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.


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);
}
Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.