【问题标题】:How to redraw DataGridView from another form如何从另一个窗体重绘 DataGridView
【发布时间】: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


【解决方案1】:

您可以使用 BindingSource 和 ShowDialog...

例子:

public partial class MainForm : Form
{
    private BindingSource bindingSource = new BindingSource();

    List<YourData> yourData = new List<YourData>();

    public MainForm()
    {
        InitializeComponent();

        bindingSource.DataSource = yourData;

        dgv.DataSource = bindingSource;
    }
}

更改将像这样反映到您的网格中...

private void EditButton_Click(object sender, EventArgs e)
{
    EditForm editForm = new EditForm(yourData);

    if (BasketGrid.RowCount > 0)
    {
        editForm.ShowDialog(this);

        bindingSource.ResetBindings(true);
    }
}

//Change your Data in EditForm whatever you want
public partial class EditForm : Form
{
    List<YourData> yourData;
    public EditForm(List<YourData> yourData)
    {
        InitializeComponent();
        this.yourData = yourData;
    }
}

【讨论】:

    【解决方案2】:

    您应该在OrderItem 类中实现INotifyPropertyChanged 接口。这将只更新DataGridView 中的一个值,而不是更新整个集合,如果集合非常大并且它的绑定可能会触发操作(如验证等),这可能很关键。

    class OrderItem : INotifyPropertyChanged
    {
        private string name;
        // other fields : price, quantity
    
        public string Name
        {
            get { return name; }
            set
            {
                if (value != name)
                {
                    name = value;
                    NotifyPropertyChanged();
                }
            }
        }
        // other properties: Price, Quantity
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    

    您还必须使用BindingList 类而不是List。支持双向数据绑定机制。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多