【问题标题】:DataGridView ComboBoxCell is not showing values that are not from the list vb.netDataGridView ComboBoxCell 未显示不在列表 vb.net 中的值
【发布时间】:2013-06-21 08:17:32
【问题描述】:

大家好,我有一个从绑定源很好地填充的组合框。所以问题是数据网格“名称”的列只显示来自填充下拉列表的值。那些与下拉列表不同的值显示为空。有人能告诉我为什么吗?下面是组合框的代码。我无法打印屏幕,但描述是这样的。 LIst 有 3 个名称:John、Jake、Jay,但该列有超过 10 个名称,每个名称都在其各自的单元格中。加载时的问题,它没有显示其他名称。

    Dim c4 As New DataGridViewComboBoxColumn()

    c4.HeaderText = "Name"
    c4.Name = "Name"
    c4.DataPropertyName = "Name"
    c4.DisplayMember = "NamesWithJ"
    c4.ValueMember = "NamesWithJ"
    c4.DisplayStyleForCurrentCellOnly = False
    c4.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing
    c4.FlatStyle = FlatStyle.Standard
    c4.SortMode = DataGridViewColumnSortMode.Automatic
    c4.DataSource = AddtBndSrc
    c4.Width = 100
    Me.DataGrid.Columns.Add(c4)

这是我创建绑定源并用值填充它的代码。完美显示的连接没有错误。只是组合框列在某些行中显示为空

                   Try

       con = New SqlConnection(strConnection)

      cib.Open()

        adoAAda = New SqlDataAdapter(StrAddNameQuery, con)

        adoAddtRs = New DataSet



        adoAAda.Fill(adoAddtRs)

        Dim tableAddt As DataTable = adoAddtRs.Tables(0)

        Dim colum As DataColumn = tableAddt.Columns(0)


        tableAddt.PrimaryKey = New DataColumn() {tableAddt.Columns(0)}

        AddtBndSrc.DataSource = adoAddtRs.Tables(0)


        con.close()

    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
    Try
        ' connection procedure
     con = New SqlConnection(strConnection)

       con.Open()

        adoPAda = New SqlDataAdapter(StrProductQuery, con)

        adoProductsRS = New DataSet



        adoPAda.Fill(adoProductsRS)

        Dim tableProduct As DataTable = adoProductsRS.Tables(0)

        Dim colum As DataColumn = tableProduct.Columns(0)


        tableProduct.PrimaryKey = New DataColumn() {tableProduct.Columns(0)}

        productBndSrc.DataSource = adoProductsRS.Tables(0)

        MsgBox(tableProduct.Columns(0).ColumnName.ToString)
       con.close

        datagridview.Datasource = productBndSrc

    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

【问题讨论】:

  • 能否分享DataGridViewdata数据源的所有代码以及绑定方式?
  • hmmmm 我不知道怎么说,但它们是 7 列,每列都是手动生成的。所以重复上面的编码。那么你想要那个还是我填充数据集和绑定源的方式??

标签: vb.net datagridview datagridcomboboxcolumn


【解决方案1】:

您想合并组合框的值。有些被硬编码到组合中,但从数据库中提取。如果是这样,您需要在绑定到组合之前将硬编码值添加到数据源中。

在下面几行之后添加下面给出的代码。

    adoAAda.Fill(adoAddtRs)
    Dim tableAddt As DataTable = adoAddtRs.Tables(0)

'在绑定之前将您的硬编码名称添加到数据源

    Dim tableAddt As DataTable = adoAddtRs.Tables(0)

    Dim dr As DataRow = tableAddt.NewRow
    dr("NamesWithJ") = "My test 1"
    tableAddt.Rows.Add(dr)
    Dim dr1 As DataRow = tableAddt.NewRow
    dr("NamesWithJ") = "My test 2"
    tableAddt.Rows.Add(dr1)

【讨论】:

  • 这不是我想要的,但它给了我一个想法。创建第二个查询,该查询来自网格视图中显示的表,并将其添加到组合框列的表中。然后使用 linq 选择 distinct 或类似的东西。谢谢你的想法
【解决方案2】:

您似乎正在将组合框 bindingSource AddtBndSrc 分配给 DataGridViewComboBoxColumn 数据源。因此,如果该列分配了一些项目,它们将被数据源覆盖。

【讨论】:

  • 那么有什么方法可以解决这个问题,我以 jay john 和 jake 为例。它实际上来自订单表中列出的产品名称。然而,作为 gridview 数据源的表具有列表没有的新产品名称。但是我真的没有权利进入并更改在数据库中保存产品名称和 ID 的表(我害怕我可能会搞砸)。所以我被困在这里任何解决方案
  • 我不知道我是否理解你。购买尝试删除 Gridview 列的预分配值,并为组合框和网格列使用相同的绑定源
  • 但是,如果它们有超过 10 种产品但只有 5 行的可能性,这意味着列表将被限制为 5 种而不是 10 种产品。所以我猜主要问题是datagridview comboboboxcell 只显示与列表相似的值。如何解决这个问题??
猜你喜欢
  • 1970-01-01
  • 2015-01-10
  • 2020-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多