【发布时间】:2017-10-23 09:56:37
【问题描述】:
我有一个包含名称、价格和数量值的项目列表。 此列表存储在一个表单中,并且此表单还有一个编辑按钮,因此当用户单击一行时,他们可以在弹出的另一个表单中编辑此项目。
我的代码可以正常工作,以便列表中的项目发生更改,但是似乎 DataGridView 只是在列表更改时没有更新。 当我编辑一个项目并添加一个新行时,它会显示更改的值。
这是我的第一个表单的代码:
private void EditButton_Click(object sender, EventArgs e)
{
EditForm editForm = new EditForm();
if (BasketGrid.RowCount > 0)
{
editForm.Show();
}
}
所以这突出设置了按钮,使其显示另一种形式。 “BasketGrid”是我的 DataGridView,在我的代码开头也给出了公共初始化(称为 dgv)
public void EditOkBut_Click(object sender, EventArgs e)
{
this.newName = editNameBox.Text;
decimal price;
int quant;
if (decimal.TryParse(editPriceBox.Text, out price))
{
this.newPrice = price;
}
else
{
MessageBox.Show("Incorrect format for price");
}
if(int.TryParse(editQuantBox.Text, out quant))
{
this.newQuantity = quant;
}
else
{
MessageBox.Show("Incorrect format for quantity");
}
foreach (OrderItem o in basketForm.GetList().ToList())
{
string listName = basketForm.getListName();
if (listName == o.ProductName)
{
o.ProductName = this.newName;
o.ProductPrice = this.newPrice;
o.ProductQuantity = this.newQuantity;
}
}
this.Close();
}
这是我的二级表单中的“编辑按钮”。这通过一种方法从我的其他表单中获取我的项目列表,并将列表中订单项的产品名称与用户从行中选择的列表名称进行比较。
我创建了“basketForm”作为我其他表单的新对象,因此我可以访问方法和内容。 我试过使用 basketForm.dgv.Refresh();但无济于事。
感谢任何帮助。
干杯, 丹尼尔
【问题讨论】:
-
如何绘制你想要重绘的DataGridView?
-
@Rekshino 据我所知,DataGridView 会在数据源更新时自行绘制。
-
您的意思是,如果您重置数据源,DataGridView 将被更新? ;) DataSource=null;DataSource=newValue;
-
@Rekshino 这是我尝试过的另一件事,在第二种形式的 foreach 循环下面。除非我把它放在错误的地方?
-
你应该在
OrderItem类中实现INotifyPropertyChanged接口。
标签: c# forms winforms datagridview