1

所以我在 Windows 窗体中创建了一个组合框。然后我进入属性并将数据源设置为数据库中的表。我将 display 和 value 成员设置为包含我想要生成为组合框项目的值的列。但是当我编译时,这组项目是空的。

我知道这个网站和互联网上有很多类似的问题,我花了几个小时尝试这些解决方案,但似乎没有任何效果。

编辑

这是windows窗体自动生成的代码。我没有编写任何影响此组合框的代码

        // fieldsBindingSource2
        // 
        this.fieldsBindingSource2.DataMember = "Fields";
        this.fieldsBindingSource2.DataSource = this.tMSDataSet;
        // 
        // FieldSelectionComboBox
        // 
        this.FieldSelectionComboBox.BackColor = System.Drawing.SystemColors.Info;
        this.FieldSelectionComboBox.DataBindings.Add(new System.Windows.Forms.Binding("SelectedItem", this.fieldsBindingSource, "Field Name", true));
        this.FieldSelectionComboBox.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.fieldsBindingSource, "Field Name", true));
        this.FieldSelectionComboBox.DataSource = this.fieldsBindingSource2;
        this.FieldSelectionComboBox.DisplayMember = "Field Name";
        this.FieldSelectionComboBox.FormattingEnabled = true;
        this.FieldSelectionComboBox.Location = new System.Drawing.Point(83, 3);
        this.FieldSelectionComboBox.MaxLength = 50;
        this.FieldSelectionComboBox.Name = "FieldSelectionComboBox";
        this.FieldSelectionComboBox.Size = new System.Drawing.Size(121, 21);
        this.FieldSelectionComboBox.TabIndex = 7;
        this.FieldSelectionComboBox.ValueMember = "Field Name";
        this.FieldSelectionComboBox.SelectedIndexChanged += new System.EventHandler(this.FieldSelectionComboBox_SelectedIndexChanged);

编辑

我不知道这是否会改变任何东西,但是组合框位于用户控件中,并且我实用地将用户控件动态添加到窗口中。

从那以后,我尝试了另一种方法。这种方式简单地从数据库中读取所有项目并将记录添加到组合框项目中。但这也不起作用。下面是我这次尝试的代码。

        SqlCommand query = new SqlCommand(SqlQuery, con);
        SqlDataReader Reader = query.ExecuteReader();
        AutoCompleteStringCollection List = new AutoCompleteStringCollection();
        while (Reader.Read())
        {
            try
            {
                List.Add(Reader.GetString(0));
            }
            catch (InvalidCastException)
            {
                int t_listItem = Reader.GetInt32(0);
                List.Add(t_listItem.ToString());
            }
        }

        NewTextBox.AutoCompleteMode = AutoCompleteMode.Suggest;
        NewTextBox.AutoCompleteCustomSource = List;

然后,我在某些正在读取的字段上出现错误。错误是,SQLException 未处理,对象名称无效。

我尝试缩小无效部分的范围,无论是类型还是长度等。但我什么也没找到。数据库中的所有值都是 varchar(50) 并且都被接受并正确输入到表中。引发异常的词的示例是“Initiation”和“Trainer”,但“[First Name]”之类的词有效。

对这两种方法的任何帮助都会很棒。

4

3 回答 3

1

请设置以下属性

this.FieldSelectionComboBox.ValueMember = "Column1"; // Will be the column name present in your database table
this.FieldSelectionComboBox.DisplayMember = "Column2"; // Will be the column name present in your database table

谢谢

马诺伊

于 2013-06-25T12:25:50.953 回答
0

我解决了这个问题。它来自另一个冲突领域的代码。

于 2013-06-25T17:19:43.537 回答
-1

这是一些可能对其他人有所帮助的 VB.Net 代码。它是返回 DataView 的通用库代码。使用 DataView 作为数据源。无需在代码中添加项目。

FieldSelectionComboBox.DataSource = GetDataView(SQL)

Function GetDataTable(ByVal SQL As String, Optional ByVal ConnectString As String = "", Optional ByVal SingleRow As Boolean = False) As DataTable ' returns read only Datatable
    Try
        If ConnectString.Length = 0 Then ConnectString = g.OISConnectString()
        Using con As New System.Data.SqlClient.SqlConnection(ConnectString)
            Dim rdr As Data.SqlClient.SqlDataReader
            con.Open()
            Dim cmd As New SqlCommand(SQL, con)
            If SingleRow Then
                rdr = cmd.ExecuteReader(CommandBehavior.SingleRow Or CommandBehavior.CloseConnection)
            Else
                rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
            End If
            Dim dt As New DataTable
            dt.Load(rdr)
            rdr.Close()
            Return dt
        End Using
    Catch ex As Exception
        MsgBox(ex.Message, , "GetDataTable")
        Return Nothing
    End Try
End Function
Function GetDataView(ByVal SQL As String, Optional ByVal ConnectString As String = "") As DataView ' returns read only Dataview
    Try
        Dim dt As DataTable
        dt = GetDataTable(SQL, ConnectString)
        If dt.Rows.Count = 0 Then
            Return Nothing
        Else
            Dim dv As New DataView(dt)
            Return dv
        End If
    Catch ex2 As NullReferenceException
        Return Nothing
    Catch ex As Exception
        MsgBox(ex.Message, , "GetDataView")
        Return Nothing
    End Try
End Function
于 2013-06-25T19:43:23.690 回答