【问题标题】:vb.net How to change the selected item of comboBox which is datagridview columnvb.net如何更改组合框的选定项,即datagridview列
【发布时间】:2013-09-19 10:37:43
【问题描述】:

如何根据表单中存在的组合框更改组合框的项目,即数据网格视图的一列

      Dim productGrid as DataGridView
      Dim objProductGroup As New DataGridViewComboBoxColumn
        With productGroup
            .HeaderText = "ProductGroup"
            .Name = "ProductGroup"
            .ReadOnly = True
            .Items.Add("Server")
            .Items.Add("Standalone")
        End With
        .Columns.Add(objProductGroup) 

我必须根据表单上的组合框选择 objProductGroup 组合框

    dim box1 as ComboBox
    box1..Items.Add("Server")
    box1.Items.Add("Standalone")

当我选择 box1 项服务器时,objProductGroup 组合框应该会自动更新。

【问题讨论】:

  • 您将 objProductGroup 创建为 TextBoxColumn ...在此之下,您使用变量 productGroup。我不确定你是否打算在 with 语句中使用 objProductGroup 变量 - 但如果你这样做了,请尝试声明一个 DAtaGridViewComboboxColumn - 你会得到更好的结果!
  • 抱歉,输入错误。我更正了这个问题,是的,我声明为 DataGridViewComboBox。哪一行代码将用于更改组合框中的项目。
  • 您处理 Combobox 的 SelectedIndexChanged 事件,然后在该函数中以编程方式更改所需的组合框。

标签: vb.net datagridview


【解决方案1】:

以下代码会将您的 DataGridView 的 CurrentRow 列“ProductGroup”更改为您在 box1 中选择的值。我不确定您是尝试将所有行设置为组合框中的值还是仅设置当前行。

在任何情况下,您都可能想测试 CurrentRow 是否真的有任何单元格。例如:

If Not productGrid.CurrentRow Is Nothing Then [Execute the value changed]

为了在我选择一行后让它工作,这是我使用的代码:

Private Sub box1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles box1.SelectedIndexChanged
    productGrid.CurrentRow.Cells("ProductGroup").Value = box1.SelectedItem
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-18
    • 1970-01-01
    • 2015-09-30
    • 2011-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多