【问题标题】:Unable to Assign Values to Combo Box from Database无法从数据库将值分配给组合框
【发布时间】:2013-07-03 07:29:01
【问题描述】:

我有一个 Windows 窗体项目,其中 em 使用组合框的窗体之一。 我有组合框的硬代码集合。场景是用户打开表单,输入信息并将其保存到数据库中,然后在特定文本框中按回车键,然后值显示从数据库中检索。

在这个表单中,我在特定的文本框上放置了“Enter key event”,当我按下 Enter 键时,它会显示数据库中的值,它适用于文本框,但不适用于组合框。 它没有显示分配给从数据库检索的组合框的值。请放置代码参考。

ComboBox1.item="some value"

【问题讨论】:

  • 很难看到这里问了什么,你能编辑一下让你的帖子尽可能清晰吗?
  • 改用comboBox1.Text = "Some value"

标签: c# winforms


【解决方案1】:

您必须先将项目插入到集合中:

int index = Combobox1.Items.Add("some value");
Combobox1.SelectedIndex = index;

【讨论】:

    【解决方案2】:

    ComboBox 上没有名为item 的属性。有一个叫做 Items 但它没有设置器。你应该在你的对象上有 DisplayMember 和 ValueMember 并设置 DataSource,或者,使用Items.Add("My option")

    例子:

     string[] stringArray = { "one", "two", "three", "four" };
     comboBox1.DataSource = stringArray;
    
              OR
     SqlCommand cmd = new SqlCommand("Select Number,Name from MyTable", conn);
     conn.Open();
     SqlDataAdapter DataA = new SqlDataAdapter(cmd);
     DataTable DSet = new DataTable();
     DataA.Fill(DSet);
     conn.Close();
     ComboBox1.DataSource = DSet;
     ComboBox1.DisplayMember = "Name";
     ComboBox1.ValueMember = "Number";
    

    【讨论】:

      【解决方案3】:

      使用 FindStringExact 方法..

      ComboBox1.SelectedIndex = ComboBox1.FindStringExact("searchvalue")
      

      参考..http://msdn.microsoft.com/en-us/library/c440x2eb.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-28
        • 1970-01-01
        • 2012-09-02
        相关资源
        最近更新 更多