简体   繁体   中英

how to add the existing row from a datagridview to another one by clicking button “enter” at the row?

i have two datagridviews,one for previewing some items and another one to store data

i just want to accept the Enter button as the double click on a row note that when i use the DoubleClick event it gives me back the row's data which is the right. but when i use the KeyDown event and use enter button it gives the info of the next row ! which is not right. here what i tried by

  private void datagridlistcust_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Close();
            }
        }

and this is how i fill the text

    TXTIDPROD.Text = FRM.DGVPRODUCTS.CurrentRow.Cells[0].Value.ToString();
    TXTNAMEPROD.Text = FRM.DGVPRODUCTS.CurrentRow.Cells[1].Value.ToString();
    TXTPRICE.Text = FRM.DGVPRODUCTS.CurrentRow.Cells[2].Value.ToString();

then finally this how i fill the second datagridview

private void TXTDISCOUNT_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                DataRow r = dt.NewRow();
               r[0] = TXTIDPROD.Text;
            r[1] = TXTNAMEPROD.Text;
            r[2] = TXTPRICE.Text;
            r[3] = TXTAMOUNT.Text;
            r[4] = TXTTOTAL.Text;
            r[5] = TXTDISCOUNT.Text;
            r[6] = TXTAFTERDIS.Text;
            dt.Rows.Add(r);
            datagridview1.DataSource = dt;

            }
        }

There is a very simple way to do this:

public Form()
{
    InitializeComponent();

    // subscribe double click event handler
    dataGrid.DoubleClick += txtHost_DoubleClick;

    // subscribe keypress event handler
    dataGrid.KeyPress += txtHost_KeyPress;
}

void dataGrid_KeyPress(object sender, KeyPressEventArgs e)
{
    // check for Enter Key Press
    if (e.KeyCode == Keys.Enter)
    { 
        // Call the double click event handler
        dataGrid_DoubleClick(sender, e);
    }
}
void dataGrid_DoubleClick(object sender, EventArgs e)
{
    // Your Code here to handle double click event
}

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