0

情况是这样的:

我有一个Form包含一个DataGridView. 即DataGridView绑定到一个对象BindingSource。绑定的对象有Property一个枚举。

我想要做的是在其中放置一列,DataGridView而不是显示枚举的数量,我想将它映射到String

有没有一种简单的方法可以做到这一点?

我正在考虑向Property返回String我想要显示的模型添加另一个,但如果可能的话,我想避免这种情况。

编辑:

是这样的enum

public enum FilterClass
{
    TypeAFilter = 1,
    TypeBFilter = 2,
    TypeCFilter = 3
};

我对 C# 世界很陌生,所以也许我做错了什么

4

4 回答 4

1

您是否想在字符串表示中获取每个枚举的名称

例如在你的课堂上做这样的事情

这只是您放置枚举声明的一个示例,请让我知道这是否是您想要的,否则我将不得不更改我的答案

namespace sampleLogin
{
    public enum FilterClass
    {
        TypeAFilter = 1,
        TypeBFilter = 2,
        TypeCFilter = 3
    };

    public partial class frmLogin : Form
    {
      public frmLogin()
      {
        InitializeComponent();

        foreach (FilterClass fltClass in Enum.GetValues(typeof(FilterClass)))
        {
            Console.WriteLine(fltClass.ToString());
        }
    }
 } 
于 2013-01-10T23:36:03.650 回答
1

假设您无法更改业务对象(假设它是第 3 方组件),您可以简单地创建一个自定义列:

private void Form1_Load(object sender, EventArgs e)
{
    // filling up example data
    var s = new List<InfoItem>();
    s.Add(new InfoItem() { PropertyA = "PA", PropertyB = 1, PropertyC = DateTime.Now, PropertyD = InfoItemType.Item_B });
    s.Add(new InfoItem() { PropertyA = "PB", PropertyB = 2, PropertyC = DateTime.Now, PropertyD = InfoItemType.Item_C });
    s.Add(new InfoItem() { PropertyA = "PC", PropertyB = 3, PropertyC = DateTime.Now, PropertyD = InfoItemType.Item_A });
    s.Add(new InfoItem() { PropertyA = "PD", PropertyB = 4, PropertyC = DateTime.Now, PropertyD = InfoItemType.Item_B });

    // assign my collection to the DataGrid
    dg.DataSource = s;

    // let's create one more column at the end
    var column = new DataGridViewColumn();
    column.CellTemplate = new DataGridViewTextBoxCell();
    column.HeaderText = "Custom Column";
    column.Name = "customColumn"; // important name to remember
    column.DataPropertyName = "PropertyD"; // the Enum Property
    dg.Columns.Add(column); // add the column
}

// let's make the use of the `CellFormatting` event
private void dg_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    // If the column is the "customColumn", check the value. 
    if (this.dg.Columns[e.ColumnIndex].Name == "customColumn")
    {
        if (e.Value != null)
        {
            // let's change to whatever we want...
            switch ((InfoItemType)e.Value)
            {
                case InfoItemType.Item_A: e.Value = "I'm A"; break;
                case InfoItemType.Item_B: e.Value = "I'm B"; break;
                case InfoItemType.Item_C: e.Value = "I'm C"; break;
                default: e.Value = "I'm not..."; break;
            }
        }
    }
}

请记住将事件附加到DataGridView对象上

在此处输入图像描述

那么结果将是这样的:

在此处输入图像描述

于 2013-01-11T00:08:56.150 回答
1

我将在 BusinessObject 类中创建一个新字段,该字段表示枚举的字符串表示并将 DataGridView 的 Column 绑定到此属性。这种方法是否满足您的要求?

public string EnumString {
  get { 
    FilterClass fClass = this.FilterClass;
    return fClasss.ToString();
  }
}
于 2013-01-10T23:28:42.433 回答
0

您可以为DataGridView.CellFormatting. 在处理程序中,第一个参数是 DataGridView(声明为对象),第二个参数是 typeDataGridViewCellFormattingEventArgs并且它有一个 property ColumnIndex。如果调用是针对正确的列索引,那么您可以从中获取row, 和,然后以您喜欢的任何方式格式化单元格。 cellDataGridView

还有一种更复杂的方法,您可以将事件处理程序分配给列,但我不会在这里重复。如果您对此方法感兴趣,请参阅我的其他帖子。

因此,在您想要显示的对象上创建另一个属性可能更简单。但是为了完整起见,我添加了这个答案。

于 2013-01-11T00:00:16.607 回答