How to define data that will be shown in gridView

Asked

Viewed 87 times

0

I have this user class ADO, where I select to return only: Registration, Name and Accesses and return in a gridView.

public List<Usuario> ObterTotalporMatricula()
    {
        var lista = new List<Usuario>();

        using (var connection = ServiceLocator.ObterConexao())
        {
            var command = connection.CreateCommand();
            command.CommandText = "SELECT MATRICULA,NOME,ACESSOS FROM USUARIO";

            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    var todos = PreenchePropriedade(reader);
                    lista.Add(todos);
                }
            }
            return lista;
        }

    }

    private Usuario PreenchePropriedade(OleDbDataReader reader)
    {
        var todos = new Usuario();

        todos.Matricula = reader.GetString(0);
        todos.Nome = reader.GetString(1);
        if (!reader.IsDBNull(2))
        {
            todos.Acessos = Convert.ToInt32(reader.GetValue(2));
        }

        return todos;
    }

}

The problem I’m having is that in my gridView it’s returning all attributes of a user entity that I have in my project. My question is how do I return only these 3 data in my gridView ??

private void ObterTotalPorMatricula()
    {
        var usuarioController = new UsuarioController();
        var lista = usuarioController.ObterTotalporMatricula();

        this.GridView1.DataSource = lista;
        this.GridView1.DataBind();

gridVIew is returning the attributes of my User + Entity

public class Usuario : EntidadeBasica
{
    public string Nome { get; set; }
    public string Matricula { get; set; }
    public string Senha { get; set; }
    public int NivelAcessoId { get; set; }
    public int ? CodigoMaleta  { get; set; }
    public string Email { get; set; }
    public int Acessos { get; set; }

}

1 answer

1


Everything you have done is correct, but there are still some adjustments:

  1. Apply the property AutoGenerateColumns of gridview for false.
  2. Create the columns in html and apply the respective property names of your user class to the fields datafield of the columns.

See an example of how to create columns and apply the datafieldon the website of MSDN.

Browser other questions tagged

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