How to hide Properties in Datagridview

Asked

Viewed 145 times

1

How can I hide my properties from my base class. I want to hide the properties in the act of loading the Datagridview from the class person, perhaps with a syntax similar to [DisplayName("Titulo")] my intention is to control what will be displayed to the user from my class and not hiding the columns in Datagridview manually!

public class Pessoa
{
   //Aqui eu quero usar uma sintaxe semelhante ao DisplayName para inativar essa properiedade la no meu DataGridView
   //Ex: [PropertyShow(false)]        
   public int id { get; set; }
   [DisplayName("Nome da Pessoa")]
   public string nome { get; set; }
}

public class Pessoas : List<Pessoa>{}

public partial class CarregaGrid
{
   void Carrega()
  {
      Pessoas colecao =  new Pessoas();
      colecao.Add(new Pessoa(){id =1, nome = "Pessoa 1"});
      colecao.Add(new Pessoa(){id =2, nome = "Pessoa 2"});
      colecao.Add(new Pessoa(){id =3, nome = "Pessoa 3"});

      //Carregando o grid
      var grid  =  new DataGridView();
      grid.DataSource = colecao;

      //Ocultando a coluna manualmente
      //Nesse ponto que quero evitar ocultar as colunas no meu View quero controlar isso na minha Classe Pessoa
      grid.Columns["id"].Visible=false;

      }
}
  • 1

    only one test: try with [Browsable(false)], in a propertyGrid works, maybe in datagrid too

  • 1

    It worked buddy. Using Browsable I can control my views. Thanks

  • @Rovann Linhalis worked for Datagridview however, stopped working my combobox using the ids of my classes, would have some other tip?

  • creates a modelView, which is basically another class just to display the data... then you can control these particularities... to facilitate the "copy" of the data between these classes, has a tool, automapper that may be useful

  • I didn’t understand very well, it would have a code fragment?

  • my application is Descktop so I didn’t understand the methodology directly in Asp.net

  • is just the idea of modelview, but could apply also in winforms or wpf. an example: https://dotnetfiddle.net/ODPz4L and to facilitate the "copying" of one object to another, could use the automapper... or make a copy manually

Show 2 more comments
No answers

Browser other questions tagged

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