如何在datagridview列中显示枚举值 [英] How to display enum values in datagridview column

查看:402
本文介绍了如何在datagridview列中显示枚举值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个数据库,而不是我的设计,但我必须使用它,它包含一个这样的表:

I have this Database, not of my design but I have to work with it, that contains a table like so :


 id  |   Name     |  status  | ...
-----+------------+----------+------
 1   |  Product1  |  2       | ...
 2   |  Product2  |  2       | ...
 3   |  Product3  |  3       | ...
 ... |  ...       |  ...     | ...

status属性指的是一个枚举,其中

The status property refers to an enum where


0 = Invalid
1 = Dev
2 = Activ
3 = Old

当我在一个只读datagridview中显示它,我希望用户看到枚举(Dev,Activ,...)的名称或一个描述,而不是数值。 datagridview绑定到一个来自DAL的datatable,而不是我的设计,所以我无法真正改变datatable。我发现如何做到这一点的唯一方法是听Datagridview.CellFormating事件,我把这段代码:

When I display this in a read-only datagridview, I would like the user to see the name of the enum (Dev, Activ, ...) or a description instead of the numeric value. The datagridview is bound to a datatable that comes from a DAL, again not of my design, so I can't really change the datatable. The only way I found how to do that is by listening to the datagridview.CellFormating event where I put this code:

private void dataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (e.ColumnIndex == 3) // column of the enum
    {
        try
        {
            e.Value = getEnumStringValue(e.Value);
        }
        catch (Exception ex)
        {
            e.Value = ex.Message;
        }
    }
}

如果我有大约1k(不是很多)或更多的项目,它需要永远...
有更好的方法来做到这一点吗?

This works fine, except that if I have around 1k (not that much) or more items, it takes forever... Is there a better way to do this ?

-Edit ---

这样工作正常,我的问题是,如果datatable中有超过1000行,它将永远是需要的。问题是CellFormating事件触发每一列,甚至不需要它的那些。让我说,我显示15列,有1000行,那么该事件触发15 000次...

---Edit---
This works fine as it is, my problem is that if there are more than 1000 rows in the datatable, it takes for ever. The problem is that the CellFormating event fires for every column, even the ones that don't need it. Let say I display 15 columns and there is 1000 rows, then that event fires 15 000 time...

有没有比使用CellFormating事件更好的方法?还是有一种方法可以将CellFormating事件添加到一个列?或者什么?

Is there a better way than using the CellFormating Event ? Or is there a way to add the CellFormating Event to only one Column ? Or what ?

推荐答案

我不会在CellFormatting上执行。我会攻击DataTable本身。我将添加一个具有枚举类型的行,并通过表添加循环并添加值。这样的东西:

I wouldn't do it on CellFormatting. I would attack the DataTable itself. I would add a row that has the type of the enum, and the loop through the table and add the values. Something like this:

    private void Transform(DataTable table)
    {
        table.Columns.Add("EnumValue", typeof(SomeEnum));
        foreach (DataRow row in table.Rows)
        {
            int value = (int)row[1]; //int representation of enum
            row[2] = (SomeEnum)value;
        }
    }

然后,在您的DataGridView中,只隐藏您的枚举的整数表示。

Then, in your DataGridView just hide the column that has the integer representation of your enum.

这篇关于如何在datagridview列中显示枚举值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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