【问题标题】:How to bind Enum to ComboBox in a DataGrid如何将枚举绑定到 DataGrid 中的 ComboBox
【发布时间】:2015-07-03 01:04:45
【问题描述】:

我在 Stack Overflow 上找到了这个,但我找不到答案。

我想将数据网格的 ComboBox 绑定到 property of a class,它返回 [one of the ] Enum Value。

MyEnum
{
    [StringValue("SomeVal")]
    SomeVal,
    [StringValue("AnotherVal")]
    AnotherVal,
    [StringValue("OneMoreVal")]
    OneMoreVal
}

class MyClass
{
    public MyEnum A_Value
    {
        return whatever; // whatever is MyEnum type
    }
}

现在我创建了一个带有组合框的数据网格列,我需要在其中绑定一个属性

myCombo.DataSource = Enum.GetValues(typeof(MyEnum)); 
myCombo.DataBindings.Add("SelectedValue", myDataSource, bindingPath + ".A_Value");

当我运行这段代码时,它失败并出现错误

“无法为没有 ValueMember 的组合框设置值”

然后我在下面添加一行

myCombo.ValueMember = "Value";

这次没有失败,但是没有设置选择值。 有人可以帮我解决这个问题吗?


我提到的:

【问题讨论】:

  • 您已标记为 WPF,但这看起来像 WinForms,并且一些链接是针对 WinForms 的。是哪个?
  • @CharlesMager 道歉,它是 WinForms

标签: c# winforms c#-4.0 datagrid


【解决方案1】:

我假设myDataSource 应该是MyClass 的一个实现... 下面是一个如何绑定它的示例。这有点冗长,但也许有人可以改进它。

public partial class Form2 : Form
{
    private MyClass one;
    private Label label1;
    private ComboBox comboBox1;
    private FlowLayoutPanel panel;
    private Button btn1;
    public Form2()
    {
        InitializeComponent();

        one = new MyClass();
        panel = new FlowLayoutPanel();
        label1 = new Label();
        comboBox1 = new ComboBox();
        btn1 = new Button();
        btn1.Text = "Click to change Property";
        btn1.Click += (sender, args) => { one.A_Value = MyEnum.BtnVal; }; // to test binding to the property

        panel.Dock = DockStyle.Fill;

        Controls.Add(panel);
        panel.Controls.Add(comboBox1);
        panel.Controls.Add(label1);
        panel.Controls.Add(btn1);

        comboBox1.SelectedIndexChanged += (sender, args) =>
        {
            one.A_Value = (MyEnum)(sender as ComboBox).SelectedItem; // update the object when the ComboBox is changed
        };

        comboBox1.DataSource = Enum.GetValues(typeof(MyEnum));
        comboBox1.DataBindings.Add("SelectedItem",one,"A_Value"); // update the ComboBox if the Property is changed by something else
        label1.DataBindings.Add("Text",one,"A_Value"); // to show that changes happen to the property and not just the ComboBox
    }



}

public enum MyEnum
{
    [Description("SomeVal")]
    SomeVal,
    [Description("AnotherVal")]
    AnotherVal,
    [Description("OneMoreVal")]
    OneMoreVal,
    [Description("ButtonClickedValue")]
    BtnVal
}

public class MyClass : INotifyPropertyChanged
{
    private MyEnum whatever;

    public MyEnum A_Value
    {
        get { return whatever; } 
        set { whatever = value;
            PropertyChanged(this, new PropertyChangedEventArgs("A_Value"));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged = delegate { };
}

【讨论】:

【解决方案2】:

据我了解,您的 datagridview 已绑定到 MyClass 列表。
如果您使用DataGridViewComboBoxColumn 类型,则使用属性DatPropertyName

'Use property name of your class to where combobox will be binding
myCombo.DataPropertyName = "A_Value" 

然后myCombo.DataSource = Enum.GetValues(typeof(MyEnum));
如果instanceOf MyClass.A_Value 的值将在组合框的数据源中 它被显示出来。

更新 如果你使用普通的ComboBox控件,那么你需要指定ValueMember属性

myCombo.ValueMember = "A_Value";

检查属性名称是否正确。因为你的问题是错误的。

【讨论】:

  • 我找不到任何属性为 DataPropertyName,因为我的是 DataGrid 而不是 DataGridView
  • Winfroms 无法控制DataGrid。除非您使用一些第三方解决方案...
猜你喜欢
  • 1970-01-01
  • 2011-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
  • 2010-10-09
相关资源
最近更新 更多