如果 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();
}