我正在设计一个pos系统,要求用户从位于另一个表单上的列表中选择产品,该列表在datagridview中生成。现在需要的是,当我单击datagridview列表上的项目时,该项目应显示在pos表单的文本框中。我有以下代码,但它没有将值传递给表单
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
POS pos = new POS();
pos.txtProductCode.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
pos.txtProductName.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
this.Hide();
}
提前致谢。
如果您使用POS表单,则应显示它。
更改“ this.Hide();” 到“ POS.Show()”
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
POS pos = new POS();
pos.txtProductCode.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
pos.txtProductName.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
pos.Show();
}
它应该工作!
编辑:
另外,如果您的POS表单已经可见,并且已从其中打开带有dataGridView的表单,那么您应该使用对所有者表单的引用。
POS形式:
private void buttonOpenProductList_Click(object sender, EventArgs e)
{
var productListForm = new ProductListForm(); // It is form with DataGridView
productListForm.Show(this); // Set owner form
}
ProductList表单(您的带有DataGridView的表单):
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
POS pos = (POS)this.Owner;
pos.txtProductCode.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
pos.txtProductName.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
this.Close();
}
ctor的示例:
POS形式:
private void buttonOpenProductList_Click(object sender, EventArgs e)
{
var productListForm = new ProductListForm(this); // It is form with DataGridView
productListForm.Show();
}
ProductList表单(您的带有DataGridView的表单):
private POS pos;
// Constructor of ProductListForm
public ProductListForm(POS pos)
{
this.pos = pos;
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
pos.txtProductCode.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
pos.txtProductName.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
this.Close();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句