我有两种表单:ProductSelectionForm
和QuantityPriceForm
ProductSelection
表单包含一个包含3列的数据网格视图(
我从Quantity
表单中获取price
和getQuantityPrice
,其中包含2个文本框,用于显示所述值。
到目前为止,我已经处理过这段代码:
Productselection Form
:
public void getQuanPrice() // call getQuantityPrice form
{
QuantityPriceForm obj = new QuantityPriceForm(this);
obj.ShowDialog();
}
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();
}
}
数据网格视图不会更新表单1中的数量和价格列。我做错了什么?
答案 0 :(得分:0)
很可能,您的产品选择表单仍然将DataGridView控件dgvProducts
设置为Private(默认值)。
您可以将其设置为公开,以便您的子表单可以访问它,但这样的风格很差。
相反,将参数传递给您的子表单并在成功时将其读回:
产品选择表:
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;
}
}
现在,在您的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();
}
}