在 DataGridView 中为 bool 和 Enum 列显示 ComboBox [英] Show ComboBox for bool and Enum columns in DataGridView

查看:26
本文介绍了在 DataGridView 中为 bool 和 Enum 列显示 ComboBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 DataTable,它包含各种数据类型的列 - intstringboolEnum(就像下面例子中的Severity):

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:

  • bool 列 → 带有真/假选项的组合框
  • 枚举列→带有枚举列表的组合框
  • 字符串列 → 就像可编辑的文本字段

推荐答案

您应该自己为这些列添加 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屋!

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