在DataGridView中显示bool和Enum列的ComboBox [英] Show ComboBox for bool and Enum columns in DataGridView
问题描述
我有一个 DataTable
,其中包含各种数据类型的列- int
,字符串
,布尔
,枚举
(例如严重程度
在下面的示例中):
I have a DataTable
which contains columns of various data types - int
, string
, bool
, Enum
( like Severity
in below example):
hostTable = new DataTable();
hostTable.Columns.Add("Suspended", typeof(bool));
hostTable.Columns.Add("Succ Tests", typeof(int));
hostTable.Columns.Add("Unsucc Tests", typeof(int));
hostTable.Columns.Add("System Name", typeof(string));
hostTable.Columns.Add("System IP", typeof(string));
hostTable.Columns.Add("Criticality", typeof(Severity));
hostTable.Columns.Add("Alert Email To", typeof(string));
hostTable.Columns.Add("Alert Email Cc", typeof(string));
hostTable.Columns.Add("Likely Impact", typeof(string));
hostTable.Columns.Add("Likely Causes", typeof(string));
hostTable.Columns.Add("Escalation", typeof(string));
hostTable.Rows.Add((bool)hd.IsSuspended, (int)hd.SuccTests, (int)hd.UnSuccTests,
hd.SystemName, hd.SystemIp, (Severity)hd.Criticality, hd.AlertEmailToAddress,
hd.AlertEmailCcAddress, hd.LikelyImpact, hd.LikelyCauses, hd.EscalationInstructions);
dgvHostTable.DataSource = hostTable;
当我将其绑定到 DataGridView
时,如何使用以下设置使列显示:
When I bind this to a DataGridView
, how can I make the columns show with this settings:
- 布尔列→具有真/假选项的ComboBox
- 枚举列→带有枚举列表的组合框
- 字符串列→就像可编辑的文本字段
推荐答案
您应该自己为这些列添加 DataGridViewComboBox
列。您可以创建一个可重用的方法来为您执行此操作。
You should add DataGridViewComboBox
column for those columns yourself. You can create a reusable method to do it for you.
在下面的代码中,我检查了 DataGridView
控件,并且对于每列,如果将其绑定到 bool
属性或 Enum
属性,则我使用 DataGridViewComboBoxCOlumn
代替:
In below code, I checked all columns of the DataGridView
control and for each column, if it was bound to a bool
property or an Enum
property, I used a DataGridViewComboBoxCOlumn
instead:
public void UseComboBoxForEnumsAndBools(DataGridView g)
{
g.Columns.Cast<DataGridViewColumn>()
.Where(x => x.ValueType == typeof(bool) || x.ValueType.IsEnum)
.ToList().ForEach(x =>
{
var index = x.Index;
g.Columns.RemoveAt(index);
var c = new DataGridViewComboBoxColumn();
c.ValueType = x.ValueType;
c.ValueMember = "Value";
c.DisplayMember = "Name";
c.DataPropertyName = x.DataPropertyName;
c.HeaderText = x.HeaderText;
c.Name = x.Name;
if (x.ValueType == typeof(bool))
{
c.DataSource = new List<bool>() { true, false }.Select(b => new
{
Value = b,
Name = b ? "True" : "False" /*or simply b.ToString() or any logic*/
}).ToList();
}
else if (x.ValueType.IsEnum)
{
c.DataSource = Enum.GetValues(x.ValueType).Cast<object>().Select(v => new
{
Value = (int)v,
Name = Enum.GetName(x.ValueType, v) /* or any other logic to get text */
}).ToList();
}
g.Columns.Insert(index, c);
});
}
示例
您只需使用以下代码测试解决方案:
You can simply test the solution using below code:
public enum MyEnum { First, Second, Third }
private void Form1_Load(object sender, EventArgs e)
{
var dt = new DataTable();
dt.Columns.Add("C1", typeof(bool));
dt.Columns.Add("C2", typeof(MyEnum));
dt.Columns.Add("C3", typeof(string));
dt.Rows.Add(true, MyEnum.First, "string");
this.dataGridView1.DataSource = dt;
UseComboBoxForEnumsAndBools(this.dataGridView1);
}
这篇关于在DataGridView中显示bool和Enum列的ComboBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!