Binding datagrid comboboxcolumn WPF

A.A Source

I have a problem when I try to bind my combobox col.

I have 2 DataSet tables and i want the combo box to show the values of 1 DataSet table but the selected index will be taken from the other DataSet.

Example :

1.Material DataSet with Id col and UnitID col that represet the unit this material weight is calculated.

2.Units DataSet With Id col and Name

The datagrid will show the Material dataset details ( id as textbox col and unitID as comboBox col)

I want my datagrid combobox col to show all Units Names in the combobox option and the selected item will be the one with the index equals to the material UnitID.

I succeeded to show all my units names as the combobox options with :

cmb.ItemsSource = DS.Units.ToList

cmb.DisplayMemberPath = "EnglishName"

But was unable to specify the SelectedValuePath and SelectedValueBinding at the right way.

c#wpfvb.netdatagridcombobox

Answers

answered 4 years ago Bhasyakarulu Kottakota #1

One data sources can be used for multiple combo boxes however two data sources can't bind to one control. Instead we can use LINQ to combine these data sources and assign resulted data source to combo box.

DataTable material= ds.Tables["Material"];
DataTable units= ds.Tables["Units"];

var query =
    from m in material.AsEnumerable()
    join u in units.AsEnumerable()
    on m.Field<int>("Column1") equals
        u.Field<int>("Column1")
select new
    {
        Text =
            u.Field<string>("EnglishName"),
        Value =
            m.Field<int>("MaterialID")
    };

cmb.ItemsSource = query.ToList();

cmb.DisplayMemberPath = "Text";
cmb.SelectedValuePath = "Value"

comments powered by Disqus