gpt4 book ai didi

c# - 无法通过 DataBoundItem 更新 DataGridView 单元格?

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

我创建了一个包含两个控件 dataGridView1button1 的表单。

public partial class Form1 : Form
{
public List<Foo> ds { get; private set; }

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
ds = new List<Foo> {
new Foo { A="abc" }, new Foo{B="bbb"}
};
dataGridView1.DataSource = new BindingList<Foo>(ds);
}

private void button1_Click(object sender, EventArgs e)
{
var foo = (Foo)dataGridView1.Rows[0].DataBoundItem;
foo.B = "1BBB";
foreach(DataGridViewRow x in dataGridView1.Rows)
{
if (x.DataBoundItem != null)
((Foo)x.DataBoundItem).C = "CCC";
}
}
}

但是,单击 button1 不会在表单中显示更改的值吗?在 Visual Studio 中调试显示已设置值。


更新:

我将代码更改为以下以使用 DataTable。但是,点击按钮仍然没有更新 UI?

public partial class Form1 : Form
{
public List<Foo> ds { get; private set; }

private Dictionary<string, Foo> dict;

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
//ds = new List<Foo> {
// new Foo { A="abc" }, new Foo{B="bbb"}
//};
//dataGridView1.DataSource = new BindingList<Foo>(ds);
var dt = new DataTable();
dt.Columns.Add(new DataColumn { DataType = typeof(string), ColumnName = "A", Unique = true });
dt.Columns.Add(new DataColumn { DataType = typeof(string), ColumnName = "B", Unique = false });
dt.Columns.Add(new DataColumn { DataType = typeof(string), ColumnName = "C", Unique = false });
dt.Columns.Add(new DataColumn { DataType = typeof(string), ColumnName = "D", Unique = false });
dt.Columns.Add(new DataColumn { DataType = typeof(string), ColumnName = "E", Unique = false });
dt.Columns.Add(new DataColumn { DataType = typeof(string), ColumnName = "F", Unique = false });
dt.Columns.Add(new DataColumn { DataType = typeof(string), ColumnName = "G", Unique = false });
dt.Columns.Add(new DataColumn { DataType = typeof(string), ColumnName = "H", Unique = false });
dt.Columns.Add(new DataColumn { DataType = typeof(string), ColumnName = "I", Unique = false });
dt.Columns.Add(new DataColumn { DataType = typeof(string), ColumnName = "J", Unique = false });
dt.Columns.Add(new DataColumn { DataType = typeof(string), ColumnName = "K", Unique = false });
dt.Columns.Add(new DataColumn { DataType = typeof(string), ColumnName = "L", Unique = false });
dt.PrimaryKey = new DataColumn[] { dt.Columns["A"] };

var row = dt.NewRow();
row["A"] = "1";
dt.Rows.Add(row);
row = dt.NewRow();
row["A"] = "2";
dt.Rows.Add(row);
row = dt.NewRow();
row["A"] = "3";
dt.Rows.Add(row);
dataGridView1.DataSource = dt;
}

private void button1_Click(object sender, EventArgs e)
{
var foo = (DataRowView)dataGridView1.Rows[0].DataBoundItem;
foo["B"] = "1BBB";
//foreach (DataGridViewRow x in dataGridView1.Rows)
//{
// if (x.DataBoundItem != null)
// ((Foo)x.DataBoundItem).C = "CCC";
//}
//dataGridView1.DataSource = ds;
}
}

最佳答案

更改值后添加“EndEdit”:

private void button1_Click(object sender, EventArgs e)
{
var foo = (DataRowView)dataGridView1.Rows[0].DataBoundItem;
foo["B"] = "1BBB";
foo.EndEdit();
}

您也可以将源更改为绑定(bind)源,然后直接在表格上工作。

关于c# - 无法通过 DataBoundItem 更新 DataGridView 单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58253734/

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