【问题标题】:Displaying only Unique values in a user control containing a combobox在包含组合框的用户控件中仅显示唯一值
【发布时间】:2015-07-07 01:44:33
【问题描述】:

我有一个包含组合框的用户控件。所有的组合框都直接绑定到同一个数据表,用户控件被创建了大约 10 次。我的问题是,用户可以从其他组合框中的组合框中选择相同的值。例如,他们在第一个组合中选择“测试”,在第二个组合中,我需要“测试”不是可用选项。所以每个组合框只能包含一个可用选项。

我知道我需要对 Combo 的 ValueChanged 事件应用逻辑,但不确定如何根据所选内容限制选项。

感谢您的帮助

【问题讨论】:

  • 它们是直接绑定到数据表,还是你加载数据并手动将其应用于组合?你用的是winforms还是WPF?
  • 它们直接绑定到Datatable,我用的是Winforms,谢谢
  • 您是否可以手动从数据表中获取数据,为每个组合框构建集合,然后以这种方式驱动它们?您必须听取每个人的更改并相应地调整另一个人,这将是相当多的手动工作,但您可能会使其通用。

标签: c# winforms combobox


【解决方案1】:

如果您在问题中分享了一些代码,那将会有所帮助。但是,这里有一些快速而肮脏的东西可能会有所帮助。当然,这可以做得更好,但正如我所说,它又快又脏。

在控件中:

public partial class ComboBoxControl : UserControl, INotifyPropertyChanged
{
    public ComboBoxControl()
    {
        InitializeComponent();
    }

    public string LabelText
    {
        get;
        set;
    }

    private string _dataBindingFilter;

    public string DataBindingFilter
    {
        get
        {
            return _dataBindingFilter;
        }
        set
        {
            if (value != _dataBindingFilter)
            {
                _dataBindingFilter = value;
                NotifyPropertyChanged("DataBindingFilter");

            }
        }
    }

    public DataTable DataSource
    {
        get;
        set;
    }

    public string DisplayMember
    {
        get;
        set;
    }

    public string ValueMember
    {
        get;
        set;
    }

    public ComboBoxControl ChildControl
    {
        get;
        set;
    }

    public void BindComboBox()
    {
        comboBox1.SelectedIndexChanged -= new EventHandler(comboBox1_SelectedIndexChanged);

        if (string.IsNullOrEmpty(DataBindingFilter))
        {
            comboBox1.DataSource = DataSource;
        }
        else
        {
            DataView view = DataSource.AsDataView();
            view.RowFilter = DataBindingFilter;
            comboBox1.DataSource = view;
        }

        comboBox1.DisplayMember = DisplayMember;
        comboBox1.ValueMember = ValueMember;
        comboBox1.SelectedIndex = -1;

        comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ChildControl != null)
        {
            ChildControl.DataSource = DataSource;
            ChildControl.ValueMember = ValueMember;
            ChildControl.DisplayMember = DisplayMember;
            ChildControl.DataBindingFilter = ChildControl.ValueMember + "<>" + comboBox1.SelectedValue;
        }

    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(string propertyName)
    {
        BindComboBox();

        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

}

在表单中,假设你有 2 个控件,那么:

comboBoxControl1.DataSource = table;
comboBoxControl1.ValueMember = "Id";
comboBoxControl1.DisplayMember = "Value";
comboBoxControl1.BindComboBox();
comboBoxControl1.ChildControl = comboBoxControl2;

假设您有一个包含数据的表对象。

【讨论】:

    【解决方案2】:

    从数据表中删除选定的行并将其绑定到第一个组合框 ChangedEvent 上的第二个组合框。

    假设你的数据表是dtTable,那么

    Private void RemoveRow()
      {
       for(int i = dtTable.Rows.Count-1; i >= 0; i--)
       {
       DataRow dr = dtTable.Rows[i];
       if (dr["name"] == comboBox1.Text) // name is the column name
        dr.Delete();
       }
      }
    

    现在将 dtTable 绑定到组合框 2

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-13
      • 2016-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-05
      • 1970-01-01
      相关资源
      最近更新 更多