C# - Name of the instance property in a Datagridview

Asked

Viewed 51 times

2

I have a class Produtos with the following attributes:

public class Produto
{
    public int cod { get; set; }
    public string codBarras { get; set; }
    public string nome { get; set; }
    public Categoria categoria { get; set; }
    public Decimal precoCompra { get; set; }
    public Decimal precoVenda { get; set; }
    public Decimal qtdEstoque { get; set; }
    public string descricao { get; set; }

}

The class Categoria is as follows:

 public class Categoria
{
    public int cod { get; set; }
    public string nome { get; set; }
}

When I wish to list the products in one DataGridView, I want the lines of Produtos have in the spine Categoria with the property nome of the class instance Categoria which is within the Produto.

But it is appearing like this at the time of listing the products in Datagridview:

Property of the "Category" column in the properties of DataGridView:

How Product instances are being created in the SELECT I do in the database :

Produto prod = new Produto();
prod....(outros campos)
prod.categoria = CategoriaDAO.getCategoria(leitorSQL.GetInt32("codCategoria"));

This getCategory() method returns an instance of the Category class according to a product category code.

Some questions:

1 - There is the possibility to do this?

2 - It is a good practice of programming to put that (in this example) objects of type Produto have an attribute of type Categoria? Or would it be better to create two variables? One for the category code and the other for the name of Categoria.

  • an option is you set the column as DataGridViewComboBoxColumn, and bind by category code, you can keep the appearance of a Textbox in the column options

1 answer

1


This answer I ended up pasting from the same English OS, from the following post: https://stackoverflow.com/a/10567944/276960

In general, it seems that Datagridview has nothing ready to do this. But your object mount is perfect (especially if you work with Entityframework).

The English OS gave an interesting solution using a Datagridview formatting event and giving the result when you use objects inside objects:

private void Grid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{

    DataGridView grid = (DataGridView)sender;
    DataGridViewRow row = grid.Rows[e.RowIndex];
    DataGridViewColumn col = grid.Columns[e.ColumnIndex];
    if (row.DataBoundItem != null && col.DataPropertyName.Contains("."))
    {
        string[] props = col.DataPropertyName.Split('.');
        PropertyInfo propInfo = row.DataBoundItem.GetType().GetProperty(props[0]);
        object val = propInfo.GetValue(row.DataBoundItem, null);
        for (int i = 1; i < props.Length; i++)
        {
            propInfo = val.GetType().GetProperty(props[i]);
            val = propInfo.GetValue(val, null);
        }
        e.Value = val;
    }
}
  • 1

    I will mark as reply because I took this code that you sent and put in the event cell_formatting of my dgv and it worked exactly as I needed in all cases . Thank you!

Browser other questions tagged

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