How can I get the values of a row when I click on a ColumnButton? (C #)

1

I would like to obtain all the values of the record in which the "delete" action applies, this should happen when I click on my columnButton.

Delete column code

private void dataTabla_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

            if (e.ColumnIndex == dataTabla.Columns["btnEliminar"].Index && e.RowIndex >= 0)
            {


/**  Aqui surge la accion de acuerdo a la columna, en este caso la columna eliminar **/


            }

}

Now I would like the values of this record to be obtained by clicking on the button.

Already I mark the index of the row, now I just need to know how I can access the data in the row with the index of it.

Update

Apparently I can already get the value and the index of my created columns that would be the buttons, but I want to obtain the value of the columns that were filled dynamically through a database.

Form of filling.

string Query = "select Id_Compra,Articulo,Categoria,Obligatorio,Costo,Adquirir from compra";
           MySqlConnection MyConn2 = new MySqlConnection(connectionString);
           MySqlCommand MyCommand2 = new MySqlCommand(Query, MyConn2);

       MySqlDataAdapter MyAdapter = new MySqlDataAdapter();
       MyAdapter.SelectCommand = MyCommand2;
       DataTable dTable = new DataTable();

       MyAdapter.Fill(dTable);
       dataTabla.DataSource = dTable;

Note: I do not know how to access them, because the indexes start from when the buttons were created and I do not know how to access the index or value of the dynamic columns

Creation of the columns of the Buttons

DataGridViewButtonColumn btnEliminar = new DataGridViewButtonColumn();
           dataTabla.Columns.Add(btnEliminar);
           btnEliminar.Text = "-";
           btnEliminar.HeaderText = "Eliminar";
           btnEliminar.Name = "btnEliminar";
           btnEliminar.Width = 60;
           btnEliminar.DefaultCellStyle.Padding = new Padding(2);
           btnEliminar.UseColumnTextForButtonValue = true;
    
asked by David 18.01.2017 в 00:19
source

3 answers

1

You can access the values using:

datagridview1.Rows
[e.RowIndex].Cells[0].
Value.ToString()

For the first column, this code would be:

datagridview1.Rows
[e.RowIndex].Cells[1].
Value.ToString()

For the second column

    
answered by 15.01.2018 в 06:20
0

Access the Rows property of the DataGridView

link

 var row = this.DataGridView1.Rows[e.RowIndex]
    
answered by 18.01.2017 в 00:59
0

You must change the "SelectionMode" property of the gridview control. This means that when a cell is selected, the entire row is marked.

Clicking on the button loads the values in the row

The code used is:

In Cells [] the index of the column you want to select is placed.

You should only place the suggested code, changing by the appropriate name of the gridview control in the click event of the button you need.

    
answered by 18.01.2017 в 02:52