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;
}
|