【问题标题】:Show ComboBox for bool and Enum columns in DataGridView在 DataGridView 中为 bool 和 Enum 列显示 ComboBox
【发布时间】:2017-03-18 22:31:13
【问题描述】:

我有一个 DataTable,其中包含各种数据类型的列 - intstringboolEnum(如以下示例中的 Severity):

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 时,如何使用此设置显示列:

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

【问题讨论】:

  • 如果你使用 DataGridView 你会自动得到一个复选框。
  • 您应该自己为这些列添加DataGridViewComboBox 列。你可以创建一个方法来为你做这件事。

标签: c# winforms datagridview combobox enums


【解决方案1】:

您应该自己为这些列添加DataGridViewComboBox 列。你可以创建一个可重用的方法来为你做这件事。

在下面的代码中,我检查了DataGridView 控件的所有列,对于每一列,如果它绑定到bool 属性或Enum 属性,我使用DataGridViewComboBoxColumn 代替:

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

示例

您可以使用以下代码简单地测试解决方案:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-16
    • 1970-01-01
    相关资源
    最近更新 更多