Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# c wpf数据网格添加行_C#_Wpf_Datagrid_Row - Fatal编程技术网

C# c wpf数据网格添加行

C# c wpf数据网格添加行,c#,wpf,datagrid,row,C#,Wpf,Datagrid,Row,我需要在表中不时插入不同列的值。列和行数据从MySql更新。每行值都位于单个MySql单元格中,格式如下: ColumnName{Delimiter}Value{BigDelimiter}Column2Name{Delimiter}Value2... 因此,我拆分单元格字符串以获得列标题和值,因为用户可以重新排列列、修改、删除或插入新列。我搜索了一个解决方案,但只得到了空行: private void GetDataTableValues() { if (dtD

我需要在表中不时插入不同列的值。列和行数据从MySql更新。每行值都位于单个MySql单元格中,格式如下:

ColumnName{Delimiter}Value{BigDelimiter}Column2Name{Delimiter}Value2...
因此,我拆分单元格字符串以获得列标题和值,因为用户可以重新排列列、修改、删除或插入新列。我搜索了一个解决方案,但只得到了空行:

    private void GetDataTableValues()
    {
        if (dtData.Value != null)
        {
            try
            {
                LoadFields();
                dgwDataMain.Items.Clear();
                dgwDataMain.Columns.Clear();
                foreach (Fields field in fields)
                {
                    DataGridTextColumn column = new DataGridTextColumn();
                    column.Header = field.name;
                    column.Binding = new Binding(field.name);
                    dgwDataMain.Columns.Add(column);
                }

                if (connection.State == System.Data.ConnectionState.Broken || connection.State == System.Data.ConnectionState.Closed)
                    connection.Open();

                command.Parameters.Clear();
                DateTime dt = dtData.Value ?? DateTime.Now;
                command.Parameters.Add("@date", MySqlDbType.Date, 50).Value = dt.ToString("yyyy-MM-dd");
                command.CommandText = "SELECT value,team FROM sessions WHERE date=@date";

                List<string> teams = new List<string>();
                foreach (Control ctrl in panDataFilter.Children)
                    if ((ctrl as CheckBox).IsChecked == true)
                        teams.Add(Convert.ToString((ctrl as CheckBox).Content));

                using (MySqlDataReader reader = command.ExecuteReader())
                {

                    while (reader.Read())
                    {
                        bool v = false;
                        if (teams.Contains(reader[1].ToString()) || teams.Count == 0)
                            v = true;
                        if (v)
                        {
                            DatabaseObject obj = new DatabaseObject();
                            List<string> str2 = new List<string>(reader[0].ToString().Split(new string[] { "</s&p>" }, StringSplitOptions.None).ToList());
                            obj.Items = new List<string>(str2.Count);
                            foreach (string str in str2)
                            {
                                List<string> item = new List<string>(str.Split(new string[] { "<&p>" }, StringSplitOptions.None).ToList());
                                int index = dgwDataMain.Columns.Single(c => c.Header.ToString() == item[0].ToString()).DisplayIndex;
                                obj.Items.Insert(index, item[1].ToString());
                            }
                            dgwDataMain.Items.Add(obj);
                        }
                    }
                }
            }
            catch (MySqlException ex)
            {
                MessageBox.Show(ex.ErrorCode.ToString() + ": " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
            }
        }
    }

    public class DatabaseObject
    {
        public List<string> Items = new List<string>();
    }
请使用Observablecollection绑定数据网格。通过使用observablecollection,您可以轻松地添加或删除项,而无需重置数据网格的数据源

示例代码:

observableCollection myClass=新建 可观察收集; myClass.addClass


要从datagrid中删除额外的行,只需使属性

Canuseraddrows="false";

你试过使用datatable吗?我讨厌使用datatable,因为每次我尝试使用它时,我在C语言中都失败了。我是WPF的新手,我还在学习。这篇文章应该回答你的问题:这正是我需要的:非常感谢!成功了。