Problems with wpf Data Grid

Asked

Viewed 33 times

3

Good night. I am developing a project but I am having problems, I use a list that is being generated with the data of my database (Entity for connection), I want to populate a data grid but I do not want all fields of my object to appear, how should I proceed? already tried dgProducts.Columns.Remove more I could not use.

class corresponding to the object products

class Produto
{
    public int Id { get; set; }
    public string Nome { get; set; }
    public int Quantidade { get; set; }
    public string Descricao { get; set; }
    public DateTime Validade { get; set; }
    public virtual ICollection<ProdutosParaProcedimentos> ListaProdutoProcedimento { get; set; }

    public Produto()
    {
    }
}

Class that takes the database data (ps know that I should use Try)

class ProdutoViewModel
{
    public static List<Produto> ExibirProdutos()
    {
        using (ConsultorioContext ctx = new ConsultorioContext())
        {
            var teste = ctx.Produtos.ToList();
            return teste;
        }      
    }
}

Xaml.Cs class of my view

private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        dgProdutos.ItemsSource = ProdutoViewModel.ExibirProdutos();
    }
}

I want to withdraw

1 answer

1


If you cannot delete hide. Change the property Visibility, of the column which it does not wish to display, to Visibility.Collapsed.

dgProdutos.Columns[/*coloque aqui o indice da coluna*/].Visibility = Visibility.Collapsed;

If you have the future intention to display this column it is best to use Visibility.Hidden so the UI reserves resources for a possible display

  • 1

    Perfect, thank you very much

Browser other questions tagged

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