How to show data from DataGridView to their respective textboxes for editting?
I'm currently making an information system that would be able to add data to an sql database. I've got the adding data done but I'm having a bit of a problem with the editing part. I want to make it easy for the user to edit the data entered when they click the row from the DGV and it would show up on the respective textboxes and when they click the "UPDATE" or "SAVE" button, it would be changed.
I'm using Micro开发者_如何学运维soft Visual Studio 2008 . C# as coding language.
Please help. Thank you! :)
In the CellClick event of your DataGridView, do this
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;
}
When the user click SAVED or UPDATE, you can write to the database, then rebind it to the DataGridView. Sorry, it's my 1st time and I didn't notice the time was 2 years ago. Still hope this will help someone in need.
You can add a button with the CommandName="Edit" CommandArgument=<%# Eval("ID") %>
Also,you can use the CommandName with Update,Cancel,Delete
精彩评论