What event do I have to use to visualize data from a DataGrid of a selected cell?


Hello, how are you? I have a method that is working well for me but it's not really what I expect.

        private void dgFleteros_CellContentClick(object sender, DataGridViewCellEventArgs e)
        DataGridViewRow row = dgFleteros.CurrentRow;
        tbNombre.Text = Convert.ToString(row.Cells[1].Value);
        tbVehiculo.Text = Convert.ToString(row.Cells[3].Value);

This works when the user clicks on the grid, but what I need is to show me the data of the cell that is currently selected, without having to click.

Thank you very much in advance

Sorry for not clarifying, it is a windows form application in visual studio and c #

asked by andeehr 15.12.2018 в 18:12

2 answers


I was able to solve it! I forgot to clarify something in the statement of the question. When I double-clicked on the design of the grid, automatically visual studio creates the method with the event of clicking the cell.

What I did was look up the references of the method that creates visual studio and change the event there

This is the one that creates:

this.dgFleteros.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgFleteros_CellContentClick);

And this is what I needed:

this.dgFleteros.SelectionChanged += new System.EventHandler(this.dgFleteros_CellContentClick);

The method that was already done remains the same, only that you have to replace the second parameter with EventArgs and

I do not know if it's the best solution but at least it works the way I want it to.

Thanks for your answers!

answered by 15.12.2018 / 21:41

You have several ways to do it, but I'll show you the simplest one. In the case of having the multiple selection disabled or if you only want to get the first one of the whole selection, you could do it like this:

dataGridView1.SelectedRows[0] // obtienes la fila seleccionada.
dataGridView1.SelectedRows[0].Index // obtienes el indice de la fila seleccionada.

If you have the active multiple selection and you want to obtain more than one row to generate a report for example, you can go through the list with a for. The code would be something like this:

// contador de filas seleccionadas.
Int32 selectedRowCount =
    if (selectedRowCount > 0) // Si hay filas seleccionadas...
       // Recorremos el listado de filas seleccionadas
        for (int i = 0; i < selectedRowCount; i++)
            // Obtienes el indice y realizas las acciones para cada uno
            // por ejemplo guardar los id en un array, cambiar un valor, etc...           


You can also check the official documentation . Greetings!

answered by 15.12.2018 в 19:23