简体   繁体   中英

display selected datagridview item to textbox on another form c#

I am designing a pos system that require user to select product from list which is located on another form, the list is generated in datagridview. what I need now is when I click on the item on the datagridview list it should display in textbox on the pos form. I have the below code but it does not pass value to the form

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

thanks in advance.

If POS is your form, you should show it.

Change 'this.Hide();' to '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();
}

It should work!

Edit:

Also, if your POS form has been visible already and form with dataGridView was opened from it, so you should use reference to owner form.

POS form:

private void buttonOpenProductList_Click(object sender, EventArgs e)
{
    var productListForm = new ProductListForm(); // It is form with DataGridView
    productListForm.Show(this); // Set owner form
}

ProductList form (your form with 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();
}

Example with ctor:

POS form:

private void buttonOpenProductList_Click(object sender, EventArgs e)
{
    var productListForm = new ProductListForm(this); // It is form with DataGridView
    productListForm.Show();
}

ProductList form (your form with 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();
}

You have to create an instance of the form you are posting to eg:

Form f2 = new Form();

f2.TextBox1.Text = somevalue;

Now you will have access to the controls such as TextBox etc on the form you are posting too. You can assign the values to them.

Please note this guide.

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

make textboxes are public

Other wise add a method to fill data in POS.

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    POS POS = null;
    FormCollection fc = Application.OpenForms;
    foreach (Form frm in fc)
    {
        if (frm.GetType().Name.Equals("POS"))
        {
            POS = frm as POS;
        }
    }
    if (POS == null)
    {
        POS = new POS();
    }
    string ProductCode = dataGridView1.CurrentRow.Cells[0].Value.ToString();
    string ProductName = dataGridView1.CurrentRow.Cells[1].Value.ToString();

    POS.FillTextBoxes(ProductCode, ProductName);
    POS.Show();
}

in POS:

internal  void FillTextBoxes(string productCode, string productName)
{
     txtProductCode.Text = productCode;
     txtProductName.Text = productName;
}

CellContentClick work when click on the text. Better to change it to dataGridView1_CellClick or CellDoubleClick.

Also POS.Show() will create a new form on each click. So apply POS.ShowDialog() if suitable for your situation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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