如何将数据从DataGridView显示到各自的文本框进行编辑?

时间:2011-04-29 03:31:01

标签: c# datagridview textbox

我目前正在制作一个能够将数据添加到sql数据库的信息系统。我已经完成了添加数据但是我对编辑部分有点问题。我希望用户能够轻松编辑从DGV单击行时输入的数据,并且它将显示在相应的文本框中,当他们单击“更新”或“保存”按钮时,它将被更改。

我正在使用Microsoft Visual Studio 2008。 C#作为编码语言。

请帮忙。谢谢! :)

2 个答案:

答案 0 :(得分:1)

在DataGridView的CellClick事件中,执行此操作

    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        //Get the selected row from user
        DataGridViewRow SelectedRow = dataGridView1.Rows[e.RowIndex];

        //Get the value at the cells in the Row
        string Username = SelectedRow.Cells["Username"].Value.ToString();
        string Password = SelectedRow.Cells["Password"].Value.ToString();

        //Set the value to the text box
        txtUsername.Text = Username;
    }

当用户单击SAVED或UPDATE时,您可以写入数据库,然后将其重新绑定到DataGridView。 对不起,这是我的第一次,我没注意到时间是2年前。仍希望这能帮助有需要的人。

答案 1 :(得分:0)

您可以添加一个带有CommandName="Edit" CommandArgument=<%# Eval("ID") %>的按钮。此外,您可以将CommandName与更新,取消,删除一起使用