简体   繁体   English

如何通过单击该行的“输入”按钮将现有行从datagridview添加到另一行?

[英]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 我有两个datagridviews,一个用于预览某些项目,另一个用于存储数据

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. 我只想接受Enter按钮作为对行的双击,请注意,当我使用DoubleClick事件时,它会给我返回正确的行数据。 but when i use the KeyDown event and use enter button it gives the info of the next row ! 但是当我使用KeyDown事件并使用Enter按钮时,它将提供下一行的信息! 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 然后最后这就是我如何填充第二个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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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