组合框问题:无法绑定到新值成员

ComboBox Issue: Cannot bind to new value member(组合框问题:无法绑定到新值成员)
本文介绍了组合框问题:无法绑定到新值成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个作为用户控件创建的组合框(它实际上由标签、组合框和文本框组成).我正在尝试将数据集绑定到组合框数据源,但在 ValueMember/Display 成员上不断收到错误消息:

I'v got a combobox that I created as a user control(it's actually made up of a label, combobox and textbox). I'm trying to bind a dataset to the combobox datasource, but I keep getting an error message on ValueMember/Display member:

Cannot bind to the new display member - newdisplay member
Cannot bind to the new value member - parameter name: value

我以为我已经为用户控件正确编码了所有内容:

I thought I had everything coded correctly for the usercontrol:

   public partial class ucComboBox : UserControl
{
    #region Properties (6) 
    private bool isEditableReadOnly;
    private bool ArrVisible;
    private string _value;
    private string _name;

    public string value
    {
        get { return _value ; }
        set { _value = value; }
    }
    public string name
    {
        get { return _name; }
        set { _name = value; }
    }
}

我在用户控件中有一些其他属性和事件,但它们不应该是问题.

I have a few other properties and events in the usercontrol but they shouldnt be the issue.

我绑定信息的代码:

  ((ucComboBox)ctrl).combobox.DataSource = info;
  ((ucComboBox)ctrl).combobox.ValueMember = "radiology_id";
  ((ucComboBox)ctrl).combobox.DisplayMember = "radiology_name";

每当它击中值成员时它就会爆炸,并且我得到上述两个错误.我的用户控件中是否缺少某些内容?在我看来这应该可以工作..(仅供参考 - 信息数据源确实包含两列)

It blows up whenever it hits value member, and i get the two errors stated above. Am I missing something in my user control? Seems to me this should be working.. (fyi - the info datasource does contain the two columns)

我尝试了很多不同的策略,但都没有成功.请帮忙!

I've tried a bunch of different tactics with no success. Please help!

谢谢

推荐答案

不再使用数据集.创建了一个包含 ID 和 Name 值的类(Facilities").将信息"更改为列表类型.然后下面的代码就可以正常工作了:

Moved away from using a Datatset. Created a class("Facilities") that will hold an ID and Name values. Changed "info" to a List type. Then the following code worked just fine:

info.Add(new Facilities { ID = dr["other_facility_id"].ToString(), Name = dr["other_facility_name"].ToString() });

  ((ucComboBox)ctrl).combobox.DataSource = new BindingSource(info, null);//info;
  ((ucComboBox)ctrl).combobox.ValueMember = "ID";
  ((ucComboBox)ctrl).combobox.DisplayMember = "Name";

绑定没有问题.

这篇关于组合框问题:无法绑定到新值成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)