Load a GridView with selected records

1

How to load a GridView into the WebForm with some rows already selected?

I'm trying the following code but without success ...

protected void BindGridAcesso(int idSenha)
        {
            Usuario usuario = IdentificadorUsuario.ObterDadosUsuario();

            var lstUsuarios = new EntidadeNegocio().ListarExcetoUsuarioLogado(usuario.UsuarioId);

            if (idSenha != 0)
            {

                var lstUsuarioSenha = new EntidadeNegocio().ListarPorSenha(idSenha);

                gdvAcesso.DataSource = lstUsuarios;
                gdvAcesso.DataBind();

                int linhasGrid = gdvAcesso.Rows.Count;

                foreach (var item in lstUsuarioSenha)
                {
                    foreach (GridViewRow gvr in gdvAcesso.Rows)
                    {
                        if (gvr.Cells[1].Text == item.NomeUsuario)
                        {
                            gvr.RowState = DataControlRowState.Selected;
                        }
                    }

                }

            }

        }

    
asked by anonymous 22.08.2016 / 20:47

1 answer

2

Use the Event RowDataBound of Grid , it allows a row-by-row configuration.

Just get your Checkbox and bookmark it.

My example looks like this:

protected void NomedoGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
      if (e.Row.RowType == DataControlRowType.DataRow)
      {
         CheckBox chkSelect = (CheckBox)e.Row.FindControl("idCheckBox");
         chkSelect.checked = true;   

      }
}
    
22.08.2016 / 21:04