C# 如何在GridDataViewer中显示属性

C# 如何在GridDataViewer中显示属性,c#,C#,情况是这样的: 我有一个表单,其中包含一个DataGridView。将DataGridView绑定到对象BindingSource。 绑定的对象有一个属性,它是一个枚举 我想做的是在DataGridView中放置一列,但不是显示枚举的编号,而是将其映射到字符串 有没有一个简单的方法可以做到这一点 我正在考虑向模型中添加另一个属性,该属性返回我想要显示的字符串,但如果可能,我希望避免这样做 编辑: enum是这样的: public enum FilterClass { TypeAFilte

情况是这样的:

我有一个
表单
,其中包含一个
DataGridView
。将
DataGridView
绑定到对象
BindingSource
。 绑定的对象有一个
属性
,它是一个枚举

我想做的是在
DataGridView
中放置一列,但不是显示枚举的编号,而是将其映射到
字符串

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

我正在考虑向模型中添加另一个
属性
,该属性返回我想要显示的
字符串
,但如果可能,我希望避免这样做

编辑:

enum
是这样的:

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

我对C#world很陌生,所以可能我做了一些完全错误的事情

我会在BusinessObject类中创建一个新字段,它表示枚举的字符串表示形式,并将DataGridView的列绑定到此属性。这种方法符合您的要求吗

public string EnumString {
  get { 
    FilterClass fClass = this.FilterClass;
    return fClasss.ToString();
  }
}

我将在BusinessObject类中创建一个新字段,该字段表示枚举的字符串表示形式,并将DataGridView的列绑定到此属性。这种方法符合您的要求吗

public string EnumString {
  get { 
    FilterClass fClass = this.FilterClass;
    return fClasss.ToString();
  }
}

是否要获取字符串表示形式中每个枚举的名称

例如,在你们班上做这样的事情

这只是一个例子,你会把枚举声明请让我知道,如果这是你想要的其他智慧我将不得不改变我的答案

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

是否要获取字符串表示形式中每个枚举的名称

例如,在你们班上做这样的事情

这只是一个例子,你会把枚举声明请让我知道,如果这是你想要的其他智慧我将不得不改变我的答案

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

您可以为
DataGridView.CellFormatting
编写事件处理程序。在处理程序中,第一个参数是DataGridView(声明为object),第二个参数的类型为
DataGridViewCellFormattingEventArgs
,它有一个属性
ColumnIndex
。如果调用的是正确的列索引,则可以从
DataGridView
中获取
行和
单元格,然后按任意方式格式化单元格

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


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

您可以为
DataGridView.CellFormatting
编写事件处理程序。在处理程序中,第一个参数是DataGridView(声明为object),第二个参数的类型为
DataGridViewCellFormattingEventArgs
,它有一个属性
ColumnIndex
。如果调用的是正确的列索引,则可以从
DataGridView
中获取
行和
单元格,然后按任意方式格式化单元格

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


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

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

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;
            }
        }
    }
}
private void Form1\u加载(对象发送方,事件参数e)
{
//填充示例数据
var s=新列表();
s、 添加(newinfoitem(){PropertyA=“PA”,PropertyB=1,PropertyC=DateTime.Now,PropertyD=InfoItemType.Item_B});
s、 添加(newinfoitem(){PropertyA=“PB”,PropertyB=2,PropertyC=DateTime.Now,PropertyD=InfoItemType.Item_C});
s、 添加(newinfoitem(){PropertyA=“PC”,PropertyB=3,PropertyC=DateTime.Now,PropertyD=InfoItemType.Item_A});
s、 添加(newinfoitem(){PropertyA=“PD”,PropertyB=4,PropertyC=DateTime.Now,PropertyD=InfoItemType.Item_B});
//将我的集合分配给DataGrid
dg.DataSource=s;
//让我们在末尾再创建一列
var column=新的DataGridViewColumn();
column.CellTemplate=新的DataGridViewTextBoxCell();
column.HeaderText=“自定义列”;
column.Name=“customColumn”;//要记住的重要名称
column.DataPropertyName=“PropertyD”;//枚举属性
dg.Columns.Add(column);//添加列
}
//让我们使用'CellFormatting'事件
私有void dg_CellFormatting(对象发送方、DataGridViewCellFormattingEventArgs e)
{
//如果该列为“customColumn”,请检查该值。
if(this.dg.Columns[e.ColumnIndex].Name==“customColumn”)
{
如果(e.Value!=null)
{
//让我们换成我们想要的。。。
开关((InfoItemType)e.Value)
{
case InfoItemType.Item_A:e.Value=“I'm A”中断;
案例InfoItemType.Item_B:e.Value=“I'm B”中断;
case InfoItemType.Item_C:e.Value=“I'm C”中断;
默认值:e.Value=“我不是……”中断;
}
}
}
}
请记住将事件附加到
DataGridView
对象

然后,结果将是这样的:


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

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;
            }
        }
    }
}
private void Form1\u加载(对象发送方,事件参数e)
{
//填充示例数据
var s=新列表();
s、 添加(new InfoItem(){PropertyA=“PA”,PropertyB=1,PropertyC=DateT