ComboBox Binding

W

web1110

Hi y'all,

I tried to bind a one column table to a ComboBox and got this error:

"Complex DataBinding accepts as a data source either an ILIST or an
ILISTSOURCE"

The code is:

DataSet dataSet1;
DataTable dataTable1 = new DataTable("LastNames");
dataTable1.Columns.Add(new DataColumn("LastNames", typeof(string)));
dataTable1.Rows.Add(new object[] {"Smith"});
dataTable1.Rows.Add(new object[] {"Jones"});
dataTable1.Rows.Add(new object[] {"Wesson"});
dataSet1.Tables.Add(dataTable1);
try
{
comboBox1.DataSource=dataSet1.Tables["LastNames"].Columns["LastNames"];
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}

But this works on a DataGrid:

dataGrid1.SetDataBinding(dataSet1, "LastNames");

Apparently I need to get an ILIST or an ILISTSOURCE for the table. Can
someone give me a pointer here?

Thanx,
Bill
 
M

Mohammad

Could you try:

comboBox1.DataSource = dataSet1.Tables["LastNames"];

comboBox1.DisplayMember = "LastNames";

And then see if it works?
 
W

web1110

Yeah, I tried that and didnn't get an error:

comboBox1.DataSource=dataSet1.Tables["LastNames"];

But the ComboBox fills with:

System.Data.DataRowView

in all rows.
 
M

Mohammad

That's strange.

Did you set the DisplayValue member as indicated in the previous post?
 
W

web1110

The exact code, copied from VS:

dataSet1=new DataSet();
DataTable dataTable2 = new DataTable("LastNames");
dataTable2.Columns.Add(new DataColumn("LastNames", typeof(string)));
dataTable2.Rows.Add(new object[] {"Smith"});
dataTable2.Rows.Add(new object[] {"Jones"});
dataTable2.Rows.Add(new object[] {"Wesson"});
dataSet1.Tables.Add(dataTable2);
dataGrid2.SetDataBinding(dataSet1, "LastNames");
try
{
comboBox1.DataSource=dataSet1.Tables["LastNames"];
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}

This gives me a ComboBox containing "System.Data.DataRowView" 3 times.
 
M

Mohammad

Then change it to:

dataSet1=new DataSet();
DataTable dataTable2 = new DataTable("LastNames");
dataTable2.Columns.Add(new DataColumn("LastNames", typeof(string)));
dataTable2.Rows.Add(new object[] {"Smith"});
dataTable2.Rows.Add(new object[] {"Jones"});
dataTable2.Rows.Add(new object[] {"Wesson"});
dataSet1.Tables.Add(dataTable2­);
dataGrid2.SetDataBinding(dataS­et1, "LastNames");
try
{
comboBox1.DataSource=dataSet1.­Tables["LastNames"];

// CODE CHANGE START HERE
comboBox1.DisplayMember = "LastNames";
// CODE CHANGES END HERE
}


catch(Exception ex)
{
MessageBox.Show(ex.Message);


}
 
G

Graham Laidler

Hi,

On the same topic as this post, can anyone see the problem with this
code?

ctl.DataBindings.Clear()
oBinding = New Binding("DataSource",
dsToBindTo.Tables("LOOKUPS").DefaultView, "ID")
ctl.DataBindings.Add(oBinding)

I'm getting the following error on the last line of code..

Complex DataBinding accepts as a data source either an IList or an
IListSource

DataViews do implement IList!!!????

Any help greatly appreciated.

G
 

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