【问题标题】:Windows Forms ComboBox Items not loading from data source as specifiedWindows 窗体组合框项目未按指定从数据源加载
【发布时间】:2013-06-24 19:25:55
【问题描述】:

所以我在 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]”之类的词有效。

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

【问题讨论】:

  • 你确定你对表有 SELECT 权限吗?如果您弄乱了其中一个字段名称,您将不会收到警告 - 它们只是不起作用。
  • 我很肯定我对数据库没有任何限制
  • 设置断点时,this.tMSDataSet的值是多少?
  • 它包含有关表的各种信息和数据库中的其他内容。前表及其列
  • this.FieldSelectionComboBox.DataSource = this.fieldsBindingSource2;中的数据和this.tMSDataSet中的数据一样吗?

标签: c# winforms combobox


【解决方案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

谢谢

马诺杰

【讨论】:

  • 如果您查看我上面提供的代码,我已经添加了这些确切的代码行。
  • 如果您的字段名称包含空格,请尝试使用 [字段名称]。或者编辑 SQL 以使用不带空格的字段名称。通常避免在字段名称中使用空格。
  • 老实说,我认为这可能会做到这一点,但不幸的是它没有改变。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    这是一些可能对其他人有所帮助的 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
    

    【讨论】:

      猜你喜欢
      • 2017-02-27
      • 1970-01-01
      • 2023-04-11
      • 2015-06-30
      • 2015-03-03
      • 1970-01-01
      • 1970-01-01
      • 2012-02-13
      • 1970-01-01
      相关资源
      最近更新 更多