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

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

问题描述

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

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

使用这段代码,我得到一个DataGridView对象,其中所有属性都是列。在我的情况下,我不想显示所有这些信息;我想显示只有两三列。我知道我可以设置



AutoGenerateColumns = false



但我不知道如何继续。
一个选项是隐藏不感兴趣的所有列,但我认为以相反的方式来做更好。我该怎么做?

解决方案

每当我这样做,我通常会使 grid.DataSource 对象上的LINQ投影的结果。



所以这样的东西:

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

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



编辑:



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



在这种情况下,您可以更好地定义一个显式视图模型并将对象投射到这些模型中。例如,

  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的一点是作为一种转换器,在模型(在你的情况下,对象列表)和视图之间进行调解。



如果您希望保留对底层对象的引用,最好的方法可能是通过构造函数提供它:

  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方法检索底层模型对象的原因是避免列为此生成。


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?

解决方案

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.

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

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