ComboBox.DataSource = {DataTable}; But after is still = Nothing

J

Jon Brunson

I've got a very weird problem, when I run the following code the
ComboBox (Me.cmbInputDevice) is always empty. The ComboBox is on a
UserControl in a Class Library. I'm using VS.NET 2003 with SourceSafe
6.0d (the problem occurs whether the code is checked in or out)

If I step through the code, The line

Me.cmbInputDevice.DataSource = dv

Executes fine (ie, doesn't error), but on the next line (after an
F8/F11) Me.cmbInputDevice.DataSource is still equal to Nothing.

dv is definately not Nothing, and is definately not empty (I can view
it's contents in the Command Window)

*if* I delete the combo box from the control, and add a new one, give it
the same name, etc, it works for a while, but then goes back to
not-working after an unknown period. This isn't any kind of practical
solution.


Sql.GetDeviceTable() returns a valid & populated DataTable

[VB.NET]

Dim dt As DataTable = Sql.GetDeviceTable()

Dim dr As DataRow = dt.NewRow()
dr("ID") = DBNull.Value
dr("Description") = "<None>"
dt.Rows.Add(dr)

Dim dv As DataView = dt.DefaultView
dv.Sort = "Description"
dv.ApplyDefaultSort = True

Me.cmbInputDevice.DataSource = dv
Me.cmbInputDevice.DisplayMember = "Description"
Me.cmbInputDevice.ValueMember = "ID"

[C#]

DataTable dt = Sql.GetDeviceTable();

DataRow dr = dt.NewRow();
dr("ID") = DBNull.Value;
dr("Description") = "<None>";
dt.Rows.Add(dr);

DataView dv = dt.DefaultView;
dv.Sort = "Description";
dv.ApplyDefaultSort = True;

Me.cmbInputDevice.DataSource = dv;
Me.cmbInputDevice.DisplayMember = "Description";
Me.cmbInputDevice.ValueMember = "ID";
 
J

Joyjit Mukherjee

Hi,

you have to set the DisplayMember and ValueMember properties of the ComboBox
alongwith the DataSource to work it properly.

Regards
Joyjit
 

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