如何在DataGridViewComboBox中显示枚举类型的成员? [英] How to show Enum type members in a DataGridViewComboBox?

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

问题描述

为了在此DatagridViewComboBox中显示 ReadAccess 枚举成员,还需要做些什么?

  ReadDataGridViewComboBoxColumn.Items.Clear(); 
ReadDataGridViewComboBoxColumn.Items.AddRange(ReadAccess.None,ReadAccess.Allowed);
ReadDataGridViewComboBoxColumn.ValueType = typeof(ReadAccess);

这里是设计师生成的有关DataGridView的代码:

  this.rolesDataGridView.AutoGenerateColumns = false; 
this.rolesDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn [] {
this.TableNameDataGridViewTextBoxColumn,
this.ReadDataGridViewComboBoxColumn,
this.WriteDataGridViewComboBoxColumn,
this.ReadCodeDataGridViewComboBoxColumn,
this.ProcessDataGridViewCheckBoxColumn,
this.AdministrateDataGridViewCheckBoxColumn});
this.rolesDataGridView.DataSource = this.bsTablePermissions;

最后,在 InitializeComponent(); ,我设置DataGridView的DataSource:

  this.rolesDataGridView.DataSource = this .RoleTablePermissions; //一个绑定源列表 


解决方案

这是一个我遇到的问题次 DataGridViewComboBoxColumn 不知道如何调和枚举的字符串表示形式与其积分值之间的差异。即使您将 ValueType 设置为枚举类型, DataGridView 将尝试将单元格的值设置为底层 int value - 这就是为什么在数据绑定期间会引发 FormatException



我发现克服这个问题的唯一方法(缺少子类化单元格类型)是将 DataGridViewComboBoxColumn 绑定到将字符串分隔的数据源来自整数值的值。您可以为此使用匿名类型:

  ReadDataGridViewComboBoxColumn.ValueType = typeof(ReadAccess); 
ReadDataGridViewComboBoxColumn.ValueMember =Value;
ReadDataGridViewComboBoxColumn.DisplayMember =显示;
ReadDataGridViewComboBoxColumn.DataSource = new ReadAccess []
{ReadAccess.None,ReadAccess.Allowed}
.Select(value => new {Display = value.ToString(),Value =(int )value})
.ToList();

这样, DataGridView 知道如何将单元格值与其格式化值相关联。


What else I have to do in order to show ReadAccess enum members in this DatagridViewComboBox?

ReadDataGridViewComboBoxColumn.Items.Clear();
ReadDataGridViewComboBoxColumn.Items.AddRange(ReadAccess.None, ReadAccess.Allowed);
ReadDataGridViewComboBoxColumn.ValueType = typeof(ReadAccess);

here is designer-generated codes about DataGridView:

this.rolesDataGridView.AutoGenerateColumns = false;
this.rolesDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.TableNameDataGridViewTextBoxColumn,
this.ReadDataGridViewComboBoxColumn,
this.WriteDataGridViewComboBoxColumn,
this.ReadCodeDataGridViewComboBoxColumn,
this.ProcessDataGridViewCheckBoxColumn,
this.AdministrateDataGridViewCheckBoxColumn});
this.rolesDataGridView.DataSource = this.bsTablePermissions;

and finally, in after InitializeComponent();, i'm setting DataGridView's DataSource:

this.rolesDataGridView.DataSource = this.RoleTablePermissions;  // a bindingsource list

解决方案

This is a problem i've come across many times. The DataGridViewComboBoxColumn doesn't know how to reconcile the difference between the enum's string representation and its integral value. Even though you set ValueType to the type of the enum, the DataGridView will try to set the cell's value to the underlying int value - this is why a FormatException will be raised during databinding.

The only way i've found to overcome this problem (short of subclassing the cell type) is to bind the DataGridViewComboBoxColumn to a data source which separates the string values from the integer values. You can use an anonymous type for this purpose:

ReadDataGridViewComboBoxColumn.ValueType = typeof(ReadAccess);
ReadDataGridViewComboBoxColumn.ValueMember = "Value";
ReadDataGridViewComboBoxColumn.DisplayMember = "Display";
ReadDataGridViewComboBoxColumn.DataSource = new ReadAccess[]
    { ReadAccess.None, ReadAccess.Allowed }
    .Select(value => new { Display=value.ToString(), Value=(int)value })
    .ToList();

This way, the DataGridView knows how to relate the cell value with its formatted value.

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

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