473,396 Members | 1,608 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Binding a ListBox to a DataTable

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
3 52830
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


"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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Dextor | last post by:
I was hoping someone else could confirm this to be a bug in the new 1.1 framework when binding to a combo box. Here is the low down: When using the Oracle DataAdapter to return a query and fill a...
3
by: Eric Lemmon | last post by:
Greetings, I have a VB.NET Windows app where I bind a listbox to a DataView column that contains date values. In this list, however, only the date (not the time) is relavent, so I want to...
2
by: Matthias | last post by:
Hi Team this may be a newbie question. I have searched the discussions before posting: I'd like to re-use a form instance to edit record details. My data binding (a DataTable bound to...
7
by: Zorpiedoman | last post by:
I'm confused. I have a listbox. The DataSource is a DataTable based on a SQL Select Statement that includes a where clause. For example, "Select ID, FirstName, LastName From Employees where...
1
by: keithb | last post by:
Can someone tell me which properties of a DataTable are used for binding to a DropDown list? I have a DataTable named dt I think that I need to bind the "EnvID" column to...
1
by: cptkirkh | last post by:
I havea listbox bound to a datatable. When the form loads it thinks the first item is selected. Is there anyway to tell the listbox when it loads not to select the first time automatically?
0
by: derndeh | last post by:
I am using VS 2003. I have two tables, BookOrders and BookInfo. BookOrders holds order info which would contain a book Id. Book info will contain the details such as a books title. I want to...
2
by: moondaddy | last post by:
Using c# 3.5, I'm trying to bind a data table to a combo box, but I cant see any data in the list. I'm sure the data table has data. Here's my code: DataSet ds = GetData(); DataTable tb =...
1
by: Aleksey Timonin | last post by:
Hi guys, I have a comboBox binded to DataTable: comboBox.DataSource = MyDataTable; comboBox.ValueMember = "id"; comboBox.DisplayMember = "id";
2
by: gorantla | last post by:
Hi, I have ta DataTable having 100+ rows,I want Bind to DataList using Server side code simply using the following code in c#. DataList.DataSource=Datatable; DataList.DataBind(); How to solve...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.