未按指定从数据源加载Windows窗体组合框项

本文关键字:窗体 Windows 组合 加载 数据源 | 更新日期: 2023-09-27 18:19:40

所以我在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"answers"Trainer",但像"[名字]"这样的词有效。

任何一种方法的帮助都将是巨大的。

未按指定从数据源加载Windows窗体组合框项

请设置以下属性

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

感谢

Manoj

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

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

FieldSelectionComboBox.DataSource=获取数据视图(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