Question : binding items collection of several comboBoxes to the Values in a specified column of the table

I have a DataSet, containing a DataTable, and a DataView of that DataTable.  I also have a WPF DataGrid which is bound to the DatView.  I wnat to bind the items collection of several ComboBoxes to the values in a specified Column of the table.

Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:

        
    

        

        

Answer : binding items collection of several comboBoxes to the Values in a specified column of the table

Fist of all we have few ComboBoxes(comboBox1, comboBox2 in code) and DataGrid(dataGrid1 in code).
Thing we're doing is binding generated data source to our DataGrid in code behind XAML using our function GetDataSource() that actualy generates source.
To bind items we're just assigning our data source to ItemSource of DataGrid(dataGrid1).

Main trick is kind of algorithm you're going to use to build your data source.
In my example with two ComboBoxes. I created object GridDataSource, witch represents values obtained from ComboBoxes by index. So basicaly our DataGrid row looks like

comboBox1.Item[0] | comboBox2.Item[0]
comboBox1.Item[1] | comboBox2.Item[1]
comboBox1.Item[2] | comboBox2.Item[2]
comboBox1.Item[3] | comboBox2.Item[3]

if one of ComboBoxes have less values then another i placed "NULL" string to my object

                if (i < comboBox2.Items.Count)
                    items.Add(((ComboBoxItem)comboBox2.Items[i]).Content.ToString());
                else
                    items.Add("NULL");

I added comments to function that generates DataSource in code snipet.

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
private List GetDataSource()
        {
            //Creating empty list of objects that would represent our DataRow in DataGrid
            List dt = new List();
            //Here we start reading values from comboboxes 
            //Just in case, expression rows = comboBox1.Items.Count > comboBox2.Items.Count ? comboBox1.Items.Count : comboBox2.Items.Count could be written like
            //if (comboBox1.Items.Count > comboBox2.Items.Count) rows = comboBox1.Items.Count else comboBox2.Items.Count
            //So basicaly "rows" variable contains maximum of elements cross comboboxes
            for (int i = 0, rows = comboBox1.Items.Count > comboBox2.Items.Count ? comboBox1.Items.Count : comboBox2.Items.Count; i < rows; i++)
            {
                //Defining temp storage for our values
                List items = new List();
                //Check If there are still items in combobox
                if (i < comboBox1.Items.Count)
                    //True - Fill item
                    items.Add(((ComboBoxItem)comboBox1.Items[i]).Content.ToString());
                else
                    //False - add null
                    items.Add("NULL");

                //Same check for next combobox
                if (i < comboBox2.Items.Count)
                    items.Add(((ComboBoxItem)comboBox2.Items[i]).Content.ToString());
                else
                    items.Add("NULL");

                //Now we would just add our future row to datasource 
                dt.Add(new GridDataSource() { cmbValue1 = items[0], cmbValue2 = items[1] });
            }
            //Returning just formed datasource
            return dt;
        }
Random Solutions  
 
programming4us programming4us