Select row of a gridview without buttons, checkbox

0

Hi, I have the following gridview that is loaded from a select, with some fields in white, I wanted to know if there is any way that when I select a record of that table I load the corresponding data in the textbox? For some reason, the select index change event does not work and the examples that I find on the internet involve creating a column that has a button to select the row

    
asked by Agustin Coronel 01.05.2018 в 04:54
source

2 answers

1

This is possible with the CellMouseClick or CellMouseDobleClick event, depending on what works best for you. An example of how to use it is the following.

private void dtg_Marca_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
    {
            int filaSeleccionada = e.RowIndex;
            marBO = new Cls_MarcaBO();

            /*agrega valores a un objeto de la clase BO mediante la fila
            seleccionada y el nombre o número de la columna de la columna*/

            //se selecciona el id para mostrar los datos en los txt

            //Ejemplo columna por número
            marBO.Id_marca = int.Parse(dtg_Marca.Rows[filaSeleccionada].Cells[0].Value.ToString());

            //Ejemplo columna por nombre
            marBO.Marca = dtg_Marca.Rows[filaSeleccionada].Cells["nombre_marca"].Value.ToString();

            //Poner valores en txt
            txt_ClaMar.Text = marBO.Id_marca.ToString();
            txt_NomMar.Text = marBO.Marca;

            //Activar/Desactivar botones en caso de requerirlo.
            btn_NuevoMar.Enabled = true;
            btn_EliminarMar.Enabled = true;
            btn_ActualizarMar.Enabled = true;
            btn_GuardarMar.Enabled = false;
            txt_NomMar.Enabled = true;

    }
    
answered by 01.05.2018 в 06:28
-3

I have this method and so it seems to work.

aspx

<asp:GridView ID="gwCodigos" OnSelectedIndexChanged="gwCodigos_SelectedIndexChanged">

.cs

protected void gwCodigos_SelectedIndexChanged(object sender, EventArgs e)
    {
        txtTipoActividad.Text = gwCodigos.SelectedRow.Cells[2].Text;
        ddlCIIUPrimario.SelectedItem.Text = gwCodigos.SelectedRow.Cells[1].Text;
    }
    
answered by 15.05.2018 в 14:31