簡體   English   中英

如何通過單擊該行的“輸入”按鈕將現有行從datagridview添加到另一行?

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

我有兩個datagridviews,一個用於預覽某些項目,另一個用於存儲數據

我只想接受Enter按鈕作為對行的雙擊,請注意,當我使用DoubleClick事件時,它會給我返回正確的行數據。 但是當我使用KeyDown事件並使用Enter按鈕時,它將提供下一行的信息! 這是不對的。 這是我嘗試過的

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

這就是我填寫文字的方式

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

然后最后這就是我如何填充第二個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;

            }
        }

有一個非常簡單的方法可以做到這一點:

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