如何显示数据从DataGridView到各自的文本框进行编辑
本文关键字:文本 编辑 DataGridView 何显示 显示 数据 | 更新日期: 2023-09-27 17:53:31
我目前正在制作一个能够将数据添加到sql数据库的信息系统。我已经完成了添加数据,但我在编辑部分有一点问题。我想让它容易为用户编辑输入的数据,当他们点击行从DGV,它会出现在各自的文本框,当他们点击"更新"或"保存"按钮,它会被改变。
我正在使用Microsoft Visual Studio 2008。c#作为编码语言。
请帮助。谢谢你!:)
在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;
}
当用户单击save或UPDATE时,您可以写入数据库,然后将其重新绑定到DataGridView。对不起,这是我第一次,我没有注意到时间是两年前。
您可以使用CommandName="Edit" CommandArgument=<%# Eval("ID") %>
添加按钮。此外,您可以使用CommandName与更新,取消,删除