简体   繁体   English

如何从另一个表单更新datagridview?

[英]How to update datagridview from another form?

I have 2 forms: a ProductSelectionForm and QuantityPriceForm 我有2种形式: ProductSelectionFormQuantityPriceForm

The ProductSelection Form contains a datagridview with 3 columns ( ProductSelection表单包含一个包含3列的datagridview(

  • Product Name 产品名称
  • Quantity 数量
  • Price 价钱

I take the Quantity and price from the getQuantityPrice form which contains 2 textboxes for the said values. 我从getQuantityPrice表单中获取“ Quantity和“ price ,该表单包含2个所述值的文本框。

so far I have worked on this code: 到目前为止,我已经在这段代码上工作了:

Productselection Form : Productselection Form

 public void getQuanPrice() // call getQuantityPrice form
    {
       QuantityPriceForm obj = new QuantityPriceForm(this);
        obj.ShowDialog();
    }

getQuantityPrice Form : getQuantityPrice Form

ProductSelection form1; // public initialization

public QuantityPriceForm(ProductSelection form_1)
    {
        InitializeComponent();
        form1 = form_1;
    }

   private void button1_Click(object sender, EventArgs e)
    {

        DialogResult saveData = MessageBox.Show("Do you want to save the data?",
      "Save Data",
      MessageBoxButtons.YesNo,
      MessageBoxIcon.Question);

        if (saveData == DialogResult.Yes)
        {
            form1.dgvProducts.CurrentRow.Cells[2].Value = quant;
            form1.dgvProducts.CurrentRow.Cells[3].Value = agreePrice;
            this.Close();
        }
    }

The data grid view does not update the quantity and price column in form 1. what am i doing wrong? 数据网格视图不会更新表格1中的数量和价格列。我在做什么错?

Chances are, your Product Selection Form still has the DataGridView control dgvProducts set to Private (the default). 可能的是,您的“产品选择表”仍将DataGridView控件dgvProducts设置为“私有”(默认值)。

You could set it to public so your child form could access it, but that is poor style. 您可以将其设置为public,以便您的子窗体可以访问它,但是样式很差。

Instead, pass parameters to your child form and read them back on success: 相反,将参数传递给您的子窗体,并在成功后将它们读回:

Product Selection Form: 产品选型表:

 public void getQuanPrice() // call getQuantityPrice form
 {
    QuantityPriceForm obj = new QuantityPriceForm(this);
    obj.quant = (int)dgvProducts.CurrentRow.Cells[2].Value;
    obj.agreePrice = (double)dgvProducts.CurrentRow.Cells[3].Value;
    if (obj.ShowDialog() == DialogResult.OK)
    {
      dgvProducts.CurrentRow.Cells[2].Value = obj.quant;
      dgvProducts.CurrentRow.Cells[3].Value = obj.agreePrice;
    }
 }

Now, on your getQuantityPrice Form, you need to create those two Public Properties: 现在,在您的getQuantityPrice表单上,您需要创建这两个公共属性:

// ProductSelection form1; (you don't need this)

public QuantityPriceForm()
{
  InitializeComponent();
}

public int quant {
  get { return (int)txtQuantity.Text; }
  set { txtQuantity.Text = value.ToString(); }
}

public int agreePrice {
  get { return (double)txtAgreePrice.Text; }
  set { txtAgreePrice.Text = value.ToString(); }
}

private void button1_Click(object sender, EventArgs e)
{
  DialogResult saveData = MessageBox.Show("Do you want to save the data?",
    "Save Data",
    MessageBoxButtons.YesNo,
    MessageBoxIcon.Question);

  if (saveData == DialogResult.OK)
  {
    this.DialogResult = saveData;
    this.Close();
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM