Keep a row selected in a DataGridView when updating

0

I would like to know what method I should use in my code so that when the DataGridView is updated, the last row selected is maintained. This is the code where I updated the dgv

Here I consult and refresh the dgv

    public void BuscarVisitas(DataGridView dgv, string clave)
    {
        using (SqlConnection conexion = Base.ObtnerCOnexion())
        {

            SqlCommand com = new SqlCommand(string.Format("SELECT * FROM Visitas WHERE Vendedor ='" + clave + "'"), conexion);
            SqlDataReader leer = com.ExecuteReader();
            int selectedIndex;
            dgv.Rows.Clear();
            dgv.Refresh();

             }
   }

Here I use an event for when I close my form the data update is done

  public void NuevaVisita_FormClosed(object sender, FormClosedEventArgs e)
    {


        if (txtVendedor.Text == "ADM")
        {
            bs.ADMBuscarVisitas2(dgvVisitas);
            this.tsAutorizar.Enabled = true;

        }
        else
        {
            bs.BuscarVisitas(dgvVisitas, txtVendedor.Text);
            this.tsAutorizar.Enabled = false;
        }
    }
    
asked by Jorge Cruz 13.11.2017 в 18:28
source

1 answer

1

You need a variable where you save the row selected by the user. Before updating the data you save the row that is selected, and just after having updated the grid with the data, you re-select the row that you saved in the variable.

For example, you declare the variable in the class that represents your form:

private int selectedRow = -1; // la inicializamos con -1 porque aun no hay fila seleccionada

Then, before loading the data from the bdd and feeding the grid with them, you save the selected row:

public void BuscarVisitas(DataGridView dgv, string clave)
{
    if (dgv.SelectedRows.Count > 0) selectedRows = dgv.SelectedRows[0].Index;
    using (SqlConnection conexion = Base.ObtnerCOnexion())
    {

And after updating you return the selection (I guess you have missing lines of code to the SearchVisit method that you put):

        dgv.Rows.Clear();
        dgv.Refresh();

         }
    if (selectedRows >=0) dgv.Rows[selectedRows].Selected = true;
}

That yes, it would be necessary to validate for example, that after the query and that the grid is filled, there is effectively data in the grid. I suppose that you have already configured your grid so that it does not accept multiple selection, and that the entire row is selected and not individual cells.

EDIT: I see that what you are looking for is that the last row is the one that should remain selected, if this is so, it is not necessary to save the row that is selected before filling the grid, it depends on how you fill the grid, if by means of daatasource or manually adding rows. If you do it using the datasource property, there is an event called databindingcomplete where you can already select the last row. If you do manual by adding rows, you select the last one when you finish adding data.

Example using the databindingcomplete event:

private void dgv_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    var lastRow = gdv.Rows.Count - 1;
    if (lastRow >= 0)
        dgv.Rows[lastRow].Selected = true;
}
    
answered by 14.11.2017 в 07:19