Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WPF DataGrid
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;
                //dtcolumn.DataType = typeof(ComboBox);
                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));
                                    //dgRepeatingGroups.Columns.Add(AddDGComboBoxColumn(_node.ChildNodes, labelchild, table, row));
                                }
                                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;
                                    }
                                }
                                //datavalue is hold value of selectedvalue for each combobox and text for each textbox.
                                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));
                        //dgRepeatingGroups.Columns.Add(AddDGComboBoxColumn(_node.ChildNodes, labelchild, table, row));
                    }
                    else if (type.ToUpper() == "DATE")
                    {
                        dgRepeatingGroups.Columns.Add(AddDataGridDatePickerColumn(labelchild, "datepicker", table, row, datavalue));
                    }
 
                }
            }
            dgRepeatingGroups.Columns.Add(AddDataGridAddActionButtonColumn("Add Group", table));
            //dgRepeatingGroups.Columns.Add(AddDataGridRemoveActionButtonColumn("Remove Group", table, row));
            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...
Posted 19-Feb-13 9:21am
DevD9191
Edited 19-Feb-13 9:24am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I got the solution. Here it is.
 
Updated C# methods.
 
private DataGridTemplateColumn AddDataGridComboBoxColumn(XmlNodeList _childnodeList, string header, DataTable table, DataRow row , string data = "")
{
DataGridTemplateColumn column = new DataGridTemplateColumn();
var comboTemplate = new FrameworkElementFactory(typeof(ComboBox));
List items = new List();
for (int count = 0; count < _childnodeList.Count; count++)
{
items.Add(_childnodeList.Item(count).InnerText.ToString());
}
 
Binding bind = new Binding(header);
bind.Mode = BindingMode.TwoWay;
 

comboTemplate.SetValue(ComboBox.ItemsSourceProperty, items);
comboTemplate.SetBinding(ComboBox.SelectedValueProperty, bind);
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;
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));
}
row[labelchild] = datavalue;
}
table.Rows.Add(row);

}
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 = 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;
}
table.Rows.Add(row);

}
}

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 George Jonsson 175
1 Kornfeld Eliyahu Peter 169
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 21 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100