Click here to Skip to main content
15,885,216 members

Change cell value in DataGrid with DataGridTemplateColumn containing ComboBox

DevD9 asked:

Open original thread
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...
Tags: C#, WPF, DataGrid

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900