I am generating Edit GUI dynamically based on XML Definition. One of the control is Tab Control with multiple tabs. Each tab is containing 1 DataGrid. DataGrid is generated based on column/values available in definition. Each column is DataGridTemplateColumn containing ComboBox or Textbox. Last column is Action column containing Button. I am able to generate first row with data based on data available in COllection. Issue is when i try to add new row, The data is still old data (as in 1st row's combobox's selected value) and new data is not selected. Here is the code.
private DataGridTemplateColumn AddDataGridComboBoxColumn(XmlNodeList _childnodeList, string header, DataTable table, DataRow row , string data = "")
{
DataGridTemplateColumn column = new DataGridTemplateColumn();
if (!table.Columns.Contains(header))
{
var comboTemplate = new FrameworkElementFactory(typeof(ComboBox));
List<string> items = new List<string>();
for (int count = 0; count < _childnodeList.Count; count++)
{
items.Add(_childnodeList.Item(count).InnerText.ToString());
}
comboTemplate.SetValue(ComboBox.ItemsSourceProperty, items);
comboTemplate.SetBinding(ComboBox.SelectedItemProperty, new Binding(data));
comboTemplate.SetValue(ComboBox.SelectedItemProperty, data);
column.Header = header;
column.CellTemplate = new DataTemplate() { VisualTree = comboTemplate };
column.CellEditingTemplate = new DataTemplate() { VisualTree = comboTemplate };
DataColumn dtcolumn = new DataColumn();
dtcolumn.Caption = header;
dtcolumn.ColumnName = header;
table.Columns.Add(dtcolumn);
row[header] = items;
}
else
column.SetCurrentValue(ComboBox.SelectedValueProperty, data);
return column;
}
private DataGrid CreatedRepeatingGroupDataGrid(XmlNodeList _childnodeList, string data)
{
DataTable table = new DataTable();
dgRepeatingGroups = new DataGrid();
dgRepeatingGroups.AutoGenerateColumns = false;
dgRepeatingGroups.CanUserAddRows = true;
dgRepeatingGroups.CanUserDeleteRows = true;
dgRepeatingGroups.HorizontalAlignment = HorizontalAlignment.Left;
if (data != "")
{
string[] arrayofRowdata = data.Split(System.Environment.NewLine.ToCharArray());
for (int i = 0; i < arrayofRowdata.Length; i++)
{
if (arrayofRowdata[i] != "")
{
if (i == 0)
{
DataRow row = table.NewRow();
foreach (XmlNode _node in _childnodeList)
{
string id = _node.Attributes.Item(0).Value;
string labelchild = _node.Attributes.Item(1).Value;
string type = _node.Attributes.Item(2).Value;
string datavalue = string.Empty;
string[] arrayofdata = arrayofRowdata[i].Split(';');
for (int i1 = 0; i1 < arrayofdata.Length; i1++)
{
if (arrayofdata[i1].Contains(labelchild.Replace(".", "")))
{
datavalue = arrayofdata[i1].Replace(" " + labelchild.Replace(".", "") + " : ", "").ToString().TrimEnd(' ');
break;
}
}
if (type.ToUpper() == "DECIMAL" || type.ToUpper() == "STRING")
{
dgRepeatingGroups.Columns.Add(AddDataGridTextColumn(labelchild, table, row, datavalue));
}
else if (type.ToUpper() == "COMBO")
{
dgRepeatingGroups.Columns.Add(AddDataGridComboBoxColumn(_node.ChildNodes, labelchild, table, row, datavalue));
}
else if (type.ToUpper() == "DATE")
{
dgRepeatingGroups.Columns.Add(AddDataGridDatePickerColumn(labelchild, "datepicker", table, row, datavalue));
}
}
}
else
{
DataRow row = table.NewRow();
table.Rows.InsertAt(row, table.Rows.Count);
foreach (XmlNode _node in _childnodeList)
{
string id = _node.Attributes.Item(0).Value;
string labelchild = _node.Attributes.Item(1).Value;
string type = _node.Attributes.Item(2).Value;
string datavalue = string.Empty;
string[] arrayofdata = arrayofRowdata[i].Split(';');
for (int i1 = 0; i1 < arrayofdata.Length; i1++)
{
if (arrayofdata[i1].Contains(labelchild.Replace(".", "")))
{
datavalue = arrayofdata[i1].Replace(" " + labelchild.Replace(".", "") + " : ", "").ToString().TrimEnd(' ');
break;
}
}
row[labelchild] = datavalue;
}
}
}
}
else
{
DataRow row = table.NewRow();
foreach (XmlNode _node in _childnodeList)
{
string id = _node.Attributes.Item(0).Value;
string labelchild = _node.Attributes.Item(1).Value;
string type = _node.Attributes.Item(2).Value;
string datavalue = string.Empty;
string[] arrayofdata = data.Split(';');
for (int i1 = 0; i1 < arrayofdata.Length; i1++)
{
if (arrayofdata[i1].Contains(labelchild.Replace(".", "")))
{
datavalue = arrayofdata[i1].Replace(" " + labelchild.Replace(".", "") + " : ", "").ToString().TrimEnd(' ');
break;
}
}
if (type.ToUpper() == "DECIMAL" || type.ToUpper() == "STRING")
{
dgRepeatingGroups.Columns.Add(AddDataGridTextColumn(labelchild, table, row, datavalue));
}
else if (type.ToUpper() == "COMBO")
{
dgRepeatingGroups.Columns.Add(AddDataGridComboBoxColumn(_node.ChildNodes, labelchild, table, row, datavalue));
}
else if (type.ToUpper() == "DATE")
{
dgRepeatingGroups.Columns.Add(AddDataGridDatePickerColumn(labelchild, "datepicker", table, row, datavalue));
}
}
}
dgRepeatingGroups.Columns.Add(AddDataGridAddActionButtonColumn("Add Group", table));
dgRepeatingGroups.Background = new SolidColorBrush(Colors.LightSkyBlue);
dgRepeatingGroups.AlternatingRowBackground = new SolidColorBrush(Colors.LightCyan);
table.AcceptChanges();
dgRepeatingGroups.ItemsSource = table.DefaultView;
return dgRepeatingGroups;
}
If I get answer for how to change selected value of ComboBox generated via DataGridTemplateColumn in second row even that would be great help...