binding a datatable to a WPF combo is not working.

  • Thread starter Thread starter moondaddy
  • Start date Start date
M

moondaddy

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 = ds.Tables["tb"];
myCombo.DataContext = tb;
myCombo.DisplayMemberPath = tb.Columns["Name"].ToString();
myCombo.SelectedValuePath = tb.Columns["ID"].ToString();

any idea what I'm doing wrong?
 
You will need to use the ItemsSource property and we also need to
explicitly convert the DataTable into a list:

DataTable dt = new DataTable();
dt.Columns.Add("Code");
dt.Columns.Add("Name");
dt.Rows.Add("c1", "n1");
dt.Rows.Add("c2", "n2");
myCombo.ItemsSource = ((IListSource)dt).GetList();
myCombo.DisplayMemberPath = "Code";
myCombo.SelectedValuePath = "Name";


Reference:

Beatriz Costa Binding to ADO.NET (http://www.beacosta.com/blog/?cat=12)


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top