使用 ComboBox WinForms C# 保持 ID 和值同步 [英] Keeping ID and Value in sync using ComboBox WinForms C#

查看:26
本文介绍了使用 ComboBox WinForms C# 保持 ID 和值同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 ComboBox 来保存数据.通常我创建 2 个 ComboBox 一个隐藏以保留 ID,一个用于保存真实数据.我需要 id 才能知道它在数据库中的哪个 id.然后我对两个复选框使用 SelectedIndex 和 event 以保持它们同步.有时我什至使用更多的组合框,并使用 SelectedIndex 和许多事件使它们彼此同步.我觉得有更好的方法.

I'm using ComboBox to keep data in. Usually i create 2 ComboBox one hidden to keep ID and one with the real data. I need id to know which id it is in database. I then use SelectedIndex and event for both checkboxes to keep them in sync. Sometimes I even use more comboboxes and keep them all in sync with each other using SelectedIndex and lots of events. I have a feeling there's a better way.

    private void czynnoscInstrumentyFinansoweComboID_SelectedIndexChanged(object sender, EventArgs e) {
        czynnoscInstrumentyFinansoweCombo.SelectedIndex = czynnoscInstrumentyFinansoweComboID.SelectedIndex;
    }

我想要的另一件事是在 ComboBox 中自动完成/搜索.当用户使用组合框并开始在组合框中输入时,它只会对第一个字母做出反应,因此当您输入开始"时,它会通过列表跳转到 s 然后到 t 然后到 a 等等,当我希望它尝试找到短语某事的开始"时.

Also another thing that I would like to have is autocomplete/search inside ComboBox. When user uses combobox and starts typing inside combobox it just reacts on first letter so when you type 'start' it will jump thru the list to s then to t then to a etc. when i would like it to try and find the phrase "Start of something".

我如何实现这两个目标?

How do I achieve both of those?

我没有使用数据集,我正在添加这样的值:

I am not using DataSets, and i am adding values like that:

  // get sql data and put it into strings/decimals then add it like that to comboBox
  string var = "sds";
  czynnoscInstrumentyFinansoweComboID.Add(var);

推荐答案

好吧,如果我正确理解您的问题,那么我会说您的感觉是正确的.您不必使用多个组合来跟踪数据/值(即您案例中的标识符).

Well, if I understand your question correctly then I'd say your feeling is correct. You dont have to use multiple combos to keep track of data/value(that is, the identifier in your case).

通常要做到这一点,我们需要为组合框分配一个数据源,这样当您获得选定的索引更改事件时,它会为您提供文本以及附加的 id.将 DisplayMember 设置为包含文本的数据集列,并将 ValueMember 属性设置为包含 ID 的数据集列.

Usually to do that we need to assign a datasource to the combo box, so that when you would get the selected index changed event, it would provide you with text as well as attached id. Set DisplayMember to your dataset column that contains text, and set ValueMember property to your dataset column that contains the ID.

DataSet yourDataSource = SomeGetDataSourceMethod(); //get your data source ready.

this.cbxLookup.DataSource = yourDataSource;
this.cbxLookup.DisplayMember ="EmployeeName";
this.cbxLookup.ValueMember = "EmployeeID";

通过 SelectedIndexChanged 事件,您可以获得 SelectedItem、SelectedIndex、SelectedValue.

Upon SelectedIndexChanged event, you can get SelectedItem, SelectedIndex, SelectedValue.

或者,您也可以执行以下操作:

Alternatively you can do following as well:

int value=1;
cbxLookup.Items.Add(new ListViewItem("Your Name", value));

--编辑 2--定义如下结构:

  class KeyValueData
  {
      public KeyValueData(string Text)
      {
          text = Text;
          itemData = 0;
      }

      public KeyValueData(string Text, int ItemData)
      {
          text = Text;
          itemData = ItemData;
      }

      public int ItemData
      {
          get
          {
              return itemData;
          }
          set
          {
              itemData = value;
          }
      }

      public override string ToString()
      {
          return text;
      }

      protected string text;
      protected int itemData;
  }

//然后添加到组合中,如下所示:

//and then add into combo like following:

comboBox1.Items.Add(new KeyValueData("New Yorkers", 21));

--编辑 1--

对于第二部分,假设您使用的是 .NET 2.0,您可以获得 KeyPress 事件并使用 ComboBox.FindString 方法.这个例子可能会有所帮助.

For your second part, assuming that you are using .NET 2.0, you can get the KeyPress event and use ComboBox.FindString method. This example might help.

这篇关于使用 ComboBox WinForms C# 保持 ID 和值同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆