Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show only certain columns in a DataGridView with custom objects

I have a DataGridView and I need to add custom objects to it. Consider the following code:

DataGridView grid = new DataGridView();
grid.DataSource = objects;

With this code I get a DataGridView object with all properties as columns. In my case, I don't want to show all of this information; I want to show just two or three columns. I know that I can set

AutoGenerateColumns = false.

But I do not know how to proceed afterwards. One option is to hide all columns that do not interest me, but I think it would be better to do it in the opposite way. How can I do this?

like image 567
Cristhian Boujon Avatar asked Feb 10 '13 02:02

Cristhian Boujon


People also ask

How to display selected Column in DataGridView in c#?

1) create a parameterised constructor in form1 which will ask a string paramter like that. this event will return you row index which is clicked and column index you have selected. now you will get the value you have selected and pass that value to the constructor of form1 for displaying it.

How to display selected columns in gridview c#?

To get the selected columns in a DataGridView controlUse the SelectedColumns property. To enable users to select columns, you must set the SelectionMode property to FullColumnSelect or ColumnHeaderSelect.


1 Answers

Whenever I do this I usually make grid.DataSource the result of a LINQ projection on the objects.

So something like this:

grid.DataSource = objects.Select(o => new
    { Column1 = o.SomeValue, Column2 = o.SomeOtherValue }).ToList();

The nice thing is that you can then set AutoGenerateColumns to true, which will generate columns based on the properties of the projected objects.

Edit:

The one downside to this approach is that by projecting everything into an anonymous object, you can have problems in situations where you need to access a specific object in a click event, for example.

In this case you may be better off defining an explicit view model and projecting your objects into those. E.g.,

class MyViewModel
{
    public int Column1 { get;set; }
    public int Column2 { get;set; }
}

grid.DataSource = objects.Select(o => new MyViewModel()
    { Column1 = o.SomeValue, Column2 = o.SomeOtherValue }).ToList();

Edit 2:

MyViewModel represents all of the columns you want to display in the DataGridView. The example properties should of course be renamed to suit what you are doing. In general, the point of a ViewModel is to serve as a sort of converter that mediates between the model (in your case your list of objects) and the view.

If you are wanting to retain a reference to the underlying object, the best way might be to supply it via the constructor:

class MyViewModel
{
    public int Column1 { get;set; }
    public int Column2 { get;set; }

    ....

    private SomeType _obj;

    public MyViewModel(SomeType obj)
    {
        _obj = obj;
    }

    public SomeType GetModel()
    {
        return _obj;
    }
}

grid.DataSource = objects.Select(o => new MyViewModel(o)
    { Column1 = o.SomeValue, Column2 = o.SomeOtherValue }).ToList();

The reason I have gone for a getter method to retrieve the underlying model object is simply to avoid a column being generated for it.

like image 152
nick_w Avatar answered Sep 22 '22 11:09

nick_w