简体   繁体   English

如何使用按钮选择最后编辑的行

[英]How do I select last edited row using a button

I have a project to register customers using a DataGridView and It has 3 buttons: 我有一个使用DataGridView注册客户的项目,它有3个按钮:

  1. "add" adds the values from the textbox “添加”添加文本框中的值
  2. "delete" deletes the selected value in the DataGridView “删除”将删除DataGridView中的选定值
  3. "new" generates the next id number (they're consecutives) and clears all the textboxes fields “新”生成下一个ID号(它们是连续的)并清除所有文本框字段

I think of adding a button named "cancel" to revert changes generated by the button "new" and it has to select the last edited row and show its cells values in the textbox. 我想添加一个名为“取消”的按钮来还原由“新”按钮生成的更改,它必须选择最后编辑的行并在文本框中显示其单元格值。 I made a "cancel" button to select the last row in the list, but the last row in the list is not always the last edited 我做了一个“取消”按钮,以选择列表中的最后一行,但是列表中的最后一行并不总是最后一次被编辑

I'm using Windows Forms, not using a database. 我正在使用Windows窗体,而不是数据库。

private void bttn_cust_cancel_Click(object sender, EventArgs e)
{

    if (dgv_customer.Rows.Count > 0)
    {
        dgv_customer.Rows[dgv_customer.Rows.Count - 2].Selected = true;
        int i;
        i = dgv_customer.SelectedCells[0].RowIndex;
        txt_cust_clave.Text = dgv_customer.Rows[i].Cells[1].Value.ToString();
        txt_cust_name.Text = dgv_customer.Rows[i].Cells[2].Value.ToString();
        txt_cust_country.Text = dgv_customer.Rows[i].Cells[3].Value.ToString();
    }
    int currentType = Convert.ToInt32(txt_cust_id.Text);
    txt_cust_id.Text = Convert.ToString(--currentType);
}

You could create a variable or class to hold the index and/or data of the last edited row. 您可以创建一个变量或类来保存最后编辑的行的索引和/或数据。 This class would be reinitialized every time the "New" button is pressed. 每次按下“新建”按钮时,将重新初始化该类。

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

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