使用自定义数据源时如何隐藏 DataGridView 的列? [英] How to hide column of DataGridView when using custom DataSource?

查看:32
本文介绍了使用自定义数据源时如何隐藏 DataGridView 的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 c# 中有一个小应用程序,它有一个 DataGridView 使用:

I have a small app in c#, it has a DataGridView that gets filled using:

grid.DataSource = MyDatasource 数组;

MyClass 保存列的结构,它看起来像这样:

MyClass hold the structure for the columns, it looks something like this:

class MyDatasource
{
    private string column1;        
    private string column2;

    public MyDatasource(string arg1, string arg2)
    {
        this.column1 = arg1;
        this.column2 = arg2;
    }

    public string column1
    {
        get
        {
            return this.column1;
        }
        set
        {
            this.column1 = value;
        }
    }

    public string column2
    {
        get
        {
            return this.column2;
        }
        set
        {
            this.column1 = value;
        }
    }
}

一切正常,DataGridView 填充了正确的数据,但现在我想隐藏 column2.我尝试在列声明上方添加 [Browsable(false)] ,这将隐藏它,但我还需要从代码中访问列值,当我使用 [Browsable(false)] 并尝试读取内容,如果该列不存在,它的行为就像这样.如果我不使用它,我可以毫无问题地读取该列,但它在 DataGridView 中可见.

Everything works fine and the DataGridView gets populated with the correct data, but now I want to hide the column2. I tried adding [Browsable(false)] above the column declaration, that will hide it, but I also need to access the column value from code, and when I use [Browsable(false)] and try to read the content it acts like if the column doesn't exist. If I don't use it I can read the column without problem but it's visible in the DataGridView.

如何隐藏该列但仍能从代码中读取其内容?

How could I hide the column but still be able to read its content from code?

推荐答案

在某些情况下,先将列添加到 DataGridView 然后再隐藏它可能是个坏主意.

In some cases, it might be a bad idea to first add the column to the DataGridView and then hide it.

例如,我有一个类,该类具有公司徽标的 Image 属性的 NHibernate 代理.如果我访问该属性(例如,通过调用其 ToString 方法在 DataGridView 中显示该属性),它将从 SQL 服务器下载图像.如果我有一个 Company 对象列表并将其用作 DataGridView 的数据源,那么(我怀疑)它会在我隐藏该列之前下载所有徽标.

I for example have a class that has an NHibernate proxy for an Image property for company logos. If I accessed that property (e.g. by calling its ToString method to show that in a DataGridView), it would download the image from the SQL server. If I had a list of Company objects and used that as the dataSource of the DataGridView like that, then (I suspect) it would download ALL the logos BEFORE I could hide the column.

为了防止这种情况,我使用了自定义属性

To prevent this, I used the custom attribute

 [System.ComponentModel.Browsable(false)]

在图像属性上,以便 DataGridView 忽略该属性(不创建列并且不调用 ToString 方法).

on the image property, so that the DataGridView ignores the property (doesn't create the column and doesn't call the ToString methods).

 public class Company
 {
     ...
     [System.ComponentModel.Browsable(false)]
     virtual public MyImageClass Logo { get; set;}

这篇关于使用自定义数据源时如何隐藏 DataGridView 的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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