如何使用自定义对象仅显示 DataGridView 中的某些列 [英] How to show only certain columns in a DataGridView with custom objects

查看:33
本文介绍了如何使用自定义对象仅显示 DataGridView 中的某些列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 DataGridView,我需要向它添加自定义对象.考虑以下代码:

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

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

通过这段代码,我得到了一个 DataGridView 对象,所有属性都作为列.就我而言,我不想显示所有这些信息;我只想显示两三列.我知道我可以设置

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?

推荐答案

每当我这样做时,我通常都会将 grid.DataSource 作为 LINQ 投影在对象上的结果.

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

就像这样:

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

好处是您可以将 AutoGenerateColumns 设置为 true,这将根据投影对象的属性生成列.

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

这种方法的一个缺点是,通过将所有内容投影到匿名对象中,例如,在需要在点击事件中访问特定对象的情况下,您可能会遇到问题.

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();

编辑 2:

MyViewModel 表示要在 DataGridView 中显示的所有列.示例属性当然应该重命名以适合您的操作.一般来说,ViewModel 的作用是充当一种转换器,在模型(在您的情况下是您的对象列表)和视图之间进行调解.

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();

我使用 getter 方法来检索底层模型对象的原因只是为了避免为其生成列.

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.

这篇关于如何使用自定义对象仅显示 DataGridView 中的某些列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆