gpt4 book ai didi

c# wpf datagrid 添加行

转载 作者:行者123 更新时间:2023-11-30 13:02:52 27 4
gpt4 key购买 nike

我需要不时地在具有不同列的表中插入值。列和行数据从 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>();
}

最佳答案

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

Canuseraddrows="false";

关于c# wpf datagrid 添加行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14194175/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com