I am working in c #, and I have data in a GRIDVIEW I want to know if it is possible that when selecting the row the textbox is filled automatically or if there is another one to do it?
I am working in c #, and I have data in a GRIDVIEW I want to know if it is possible that when selecting the row the textbox is filled automatically or if there is another one to do it?
I know one way, it is useful and quite simple, first of all, it involves two events:
dataGridView1_CellClick
dataGridView1_CellDoubleClick
The first method makes that when you select a row, whatever it is (by clicking on it) the row index ( RowIndex
) is stored, it would be like this
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
renglon = e.RowIndex; //La variable renglon debe estar previamente declarada y ser de tipo entero
}
Once you have the index of the row that is selected, you only extract the data of dataGridView
:
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
string nombre;
nombre = dataGridView1.Rows[renglon_selec].Cells["NOMBRE"].Value.ToString();
txtNombre.Text = nombre; //Aqui le asignas al TextBox lo que sacaste del dgv
}
For the other fields you just have to do the same.
If it worked for you, do not forget to mark it: D
You can add the onrowcommand attribute to your GridView and get the values with the GridViewCommandEventArgs that the event receives As a reference you can take the answers to a question that was asked some time ago
When you get the value you need, you can assign it to the TextBox using its runat="server" attribute, which is added so that it can be referenced from your C # code