C# WPF 数据表格添加行

4

我需要不定时向一个具有不同列的表格中插入值。这些列和行数据来自于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>();
    }

你尝试过使用数据表吗? - Nicolas Pierre
我讨厌使用 DataTable,在 C# 中每次尝试使用它都失败了。我是 WPF 的新手,仍在学习中。 - ice 13
1
这篇SO帖子应该能回答你的问题:将DataGrid绑定到ObservableCollection<Dictionary> - Sphinxxx
2个回答

0
请使用ObservableCollection来绑定数据表格。通过使用ObservableCollection,您可以轻松地添加或删除项目,而无需重置数据表格的数据源。
示例代码:

observableCollection myClass = new observableCollection(); myClass.add(Class)


0

要从数据网格中删除额外的行,只需设置属性...

Canuseraddrows="false";

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接