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>();
    }
4

2 回答 2

0

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

Canuseraddrows="false";
于 2013-07-30T09:31:28.413 回答
0

请使用Observablecollection绑定数据网格。通过轻松使用observablecollection,您可以添加或删除项目,而无需重置数据网格的数据源。

示例代码:

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

于 2013-01-16T04:51:31.970 回答