无法将ComboBox SelectedValue属性与object属性绑定

时间:2013-12-25 05:48:00

标签: c# winforms data-binding combobox

我想将comboBox SelectedValue属性绑定到我的class property之一,这里是代码

  ComboBox1.DataBindings.Add("SelectedValue", _bindingClass, "ID",true);
  //Also tried
  ComboBox1.DataBindings.Add("SelectedValue", _bindingClass, "ID");

类对象结构如下:

public class BindingClass
{
    public long ID { get; set; }
    public string Code { get; set; }
    public string Name { get; set; }
}

这是ccomboBox绑定的代码

   public static void BindDataSourceWithCombo(ref ComboBox cmb)
    {
        DataTable _tableSource = (new AccessConnectionManager()).GetDataTableBySQLQuery("select ValueID,ValueName from PicklistValues");

        var _dataSource = (from DataRow _row in _tableSource.Rows
                           select new
                           {
                               ValueMember = _row["ValueID"],
                               DisplayMember = _row["ValueName"].ToString()

                           }).ToList();

        cmb.DataSource = _dataSource;
        cmb.ValueMember = "ValueMember";
        cmb.DisplayMember = "DisplayMember";
    }

ComboBox的ValueMemberintDisplayMemberstring

但是当我将comboBox显示文本设置为空并且comboBox上没有显示值时,它不起作用。

修改

当我在数据库中搜索ValueID的DataType时,我发现它是Int16,而在类属性中我使用Int64进行了分配。更改ID属性问题的dataType后得到解决。

3 个答案:

答案 0 :(得分:1)

这里的基本问题是组合框上的SelectedValue属性将是BindingClass类型,并且您尝试将其持久化为整数字段。

我会将SelectedValue占位符对象从BindingClass类型中取出 - 无论如何尝试存储该值都没有意义。相反,将它存储在代码隐藏的表单中,或者更好的是,如果您使用MVVM将其存储在ViewModel中。

Public class MyViewModel
{
    public ObservableCollection<BindingClass> { get; set; }       
    public BindingClass SelectedBindingClass {get; set; }
}

答案 1 :(得分:1)

public partial class Form1 : Form
{
    private void Form1_Load(object sender, EventArgs e)
    {
        BindingList<BindingClass> list = new BindingList<BindingClass>();
        list.Add(new BindingClass { ID = 1, Name = "bc1" });
        list.Add(new BindingClass { ID = 2, Name = "bc2" });
        list.Add(new BindingClass { ID = 3, Name = "bc3" });
        comboBox1.DataSource = list;

        // If you want bind to class use this code. Selected property will contain BindingClass instance, selected in combobox
        //comboBox1.DisplayMember = "Name";
        //comboBox1.DataBindings.Add("SelectedValue", this, "Selected", true, DataSourceUpdateMode.OnPropertyChanged);

        // Use label to view Selected value
        //label1.DataBindings.Add("Text", this, "Selected", true, DataSourceUpdateMode.OnPropertyChanged);

        // OR

        // If you want bind to ID use this code. SelectedId property of the form will contain ID value, selected in combobox
        comboBox1.DisplayMember = "Name";
        comboBox1.ValueMember = "ID";
        comboBox1.DataBindings.Add("SelectedValue", this, "SelectedId", true, DataSourceUpdateMode.OnPropertyChanged);

        // Use label to view SelectedId value
        label1.DataBindings.Add("Text", this, "SelectedId", true, DataSourceUpdateMode.OnPropertyChanged);
    }

    public BindingClass Selected { get; set; }

    public int SelectedId { get; set; }
}

答案 2 :(得分:1)

将您的班级更改为使用int而不是long

public class BindingClass
{
  public int ID { get; set; }
  public string Code { get; set; }
  public string Name { get; set; }
}

您的匿名linq很可能将该值解释为整数,而不是长整数,并且它会干扰DataBinding引擎。