使用包含ComboBox的DataGridTemplateColumn更改DataGrid中的单元格值 [英] Change cell value in DataGrid with DataGridTemplateColumn containing ComboBox

查看:67
本文介绍了使用包含ComboBox的DataGridTemplateColumn更改DataGrid中的单元格值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在基于XML定义动态生成Edit GUI。其中一个控件是带有多个选项卡的Tab Control。每个选项卡包含1个DataGrid。 DataGrid基于定义中可用的列/值生成。每列都是包含ComboBox或Textbox的DataGridTemplateColumn。最后一列是包含Button的Action列。我能够根据COllection中可用的数据生成包含数据的第一行。问题是当我尝试添加新行时,数据仍然是旧数据(如第1行组合框的选定值),并且未选择新数据。这是代码。

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





如果我得到如何更改第二行中通过DataGridTemplateColumn生成的ComboBox的选定值的答案,即使是很有帮助...



If I get answer for how to change selected value of ComboBox generated via DataGridTemplateColumn in second row even that would be great help...

推荐答案

我得到了解决方案。这里是。



更新了C#方法。



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<string> items = new List<string>();
                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;
        }


这篇关于使用包含ComboBox的DataGridTemplateColumn更改DataGrid中的单元格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆