how to select data from a datagrid

0

I have a datagrid and within that datagrid I have 3 columns, 2 of which receive the information from a database and the third one is typed by the user inside those columns I have several rows, what I want to do is that when I select a row that data shows me in 3 textbox which 2 are blocked and the third is where the value is typed, for the moment I have that you select any cell and gives me the value of that cell in a textbox but I do not need that.

    
asked by Cristian R.M 21.07.2016 в 20:28
source

4 answers

1
var row = dvgDatos.CurrentRow;

this returns the selected row, then to dump the data in the textbox flames of the row the cell you need.

var value = string.empty;
value = row["nombreDelaColumna"].toString();
//O Por indice 
var indexColumna = 0;//Índice de la columna en el datagrid
value = row[indexColumna].toString();
    
answered by 21.07.2016 в 20:56
1

We subscribed to the CellClick event and in the method we will use this:

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    textBox1.Text = (string)dataGridView1.Rows[e.RowIndex].Cells[0].Value;
    textBox2.Text = (string)dataGridView1.Rows[e.RowIndex].Cells[1].Value;
    textBox3.Text = (string)dataGridView1.Rows[e.RowIndex].Cells[2].Value;
}

I advise you to create a method, something that makes it elegant now that we know how to access the cells of the row you have selected.

    
answered by 22.07.2016 в 06:35
0

Because you do not change this datagrid for a dataGridView, it is an improved control of the classic datagrid, and you will avoid problems like this one.

    
answered by 05.08.2016 в 10:20
0

I've done it with Visual Basic .NET but here I'll try to show it in C #

Use the SelectedIndexChanged event of the GridView which will be activated each time you select a row or a record.

Now, as a first step, get the row number and save it in a variable called, for example, RowNumber

    NumeroDeFila = GridView1.SelectedRow.RowIndex

Next, get the value of the cells in that row and store them in their respective text boxes.

    textBox1.Text = GridView1.Rows[NumeroDeFila].Cells[0].Text;
    textBox2.Text = GridView1.Rows[NumeroDeFila].Cells[1].Text;
    textBox3.Text = GridView1.Rows[NumeroDeFila].Cells[2].Text;
    
answered by 21.12.2018 в 17:15