How to define data that will be shown in the gridView

0

I have this class userDAO, where I make a select to return only: Enroll, 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 is returning all attributes of a user entity 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();
  

The gridVIew is returning the attributes of my entity EntidadeBasica

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; }

}
    
asked by anonymous 14.10.2014 / 15:35

1 answer

1

Everything you've done is correct, but some adjustments are still missing:

  • Apply the property AutoGenerateColumns from gridview to false .
  • Create the columns in the html and apply their property names of your user class to the% of columns% fields.
  • See an example of how to create the columns and apply datafield to the MSDN .

        
    14.10.2014 / 16:55