Load a Gridview with selected records

Asked

Viewed 191 times

1

How to load a Gridview into the webform with a few lines 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;
                        }
                    }

                }

            }

        }

inserir a descrição da imagem aqui

  • I didn’t understand that your selected line would be colored or something?

  • edited the post with the image...would be with the checkbox of the selected line

1 answer

2


Use the Event Rowdatabound of Grid, it allows a line-to-line configuration.

Just you get your Checkbox and mark it.

My example was 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;   

      }
}
  • 1

    opa valeu gave it right here !

  • 1

    @Daniel glad I could help you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.