vb.net - 在 DatagridView VB.Net 中填充 ComboBox 列

标签 vb.net visual-studio-2012 datagridview

我有一个 datagridview,其中有 2 列作为组合框,我想根据第一个列填充第二个列。

例如。我的数据库中有一个包含电台的表

TableStations
Station 1 
Station 2

每个站的输出量不同

例如。

Station 1      Station 2
OutP1            OutP5
OutP2            OutP6
                 OutP7

我想在 datagridview 中做的是,当用户从第一个组合框中选择一个电台时,下一个组合框将填充该电台的输出,当用户在 datagridview 中添加第二行时,我的问题就出现了,如果他选择不同的电台,第一行的信息将被修改。

是否有任何解决方案或任何其他方式来做我想做的事?

提前致谢

编辑:这是我使用的代码

                Con.Open()
                cmd.Parameters.Clear()
                With cmd
                    .CommandText = "Select output From List_outputs where station=@station"               
                    .Parameters.AddWithValue("@station", datagridview1.Item(0, e.RowIndex).Value)
                    .Connection = Con
                    reader = .ExecuteReader
                End With
                combobox2.Items.Clear()
                While reader.Read
                    combobox2.Items.Add(reader("output "))
                End While
                reader.Close()

此代码位于我的 datagridview 的 cellclick 事件下。

最佳答案

这有点棘手,因为您无法设置列的数据源。设置列的数据源会影响整个列。您必须单独设置每个单元格的数据源。我将向您展示如何做到这一点。

首先在空表单中添加一个DataGridView。不要添加列,我们将通过代码添加列。您不必在实际项目中通过代码添加列,但请按照我在本示例中所做的操作。我添加注释以使代码易于理解。我选择创建两个类来保存 Station 和 Output。这也是可选的,您可以只使用 DataReader 并手动添加它们。希望这对您有帮助。

Public Class Form1

    Dim outputs As List(Of Output) ' this holds the fake output data.

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        ' Replace this section with the code to retrieve stations from database.
        Dim stations As New List(Of Station) From {
            New Station() With {.StationName = "Station 1"},
            New Station() With {.StationName = "Station 2"}
        }

        ' Add stations to first combobox
        Dim firstColumn = New DataGridViewComboBoxColumn()
        For Each station In stations
            firstColumn.Items.Add(station.StationName)
        Next

        ' Populate fake data, replace this section with the code to retrive outputs from database.
        outputs = New List(Of Output) From {
            New Output() With {.OutputName = "OutP1", .StationName = "Station 1"},
            New Output() With {.OutputName = "OutP2", .StationName = "Station 1"},
            New Output() With {.OutputName = "OutP5", .StationName = "Station 2"},
            New Output() With {.OutputName = "OutP6", .StationName = "Station 2"},
            New Output() With {.OutputName = "OutP7", .StationName = "Station 2"}
        }

        ' add combobox columns to datagridview
        DataGridView1.Columns.Add(firstColumn)
        DataGridView1.Columns.Add(New DataGridViewComboBoxColumn())

    End Sub

    Private Sub DataGridView1_CellBeginEdit(sender As Object, e As DataGridViewCellCancelEventArgs) Handles DataGridView1.CellBeginEdit

        ' Only process if the column is the second combobox.
        ' You will need to change the index according to your second combobox index.
        ' e.ColumnIndex = 1 because the second combobox index is 1 in this sample.
        If e.ColumnIndex = 1 Then

            ' Filter the outputs by selected Station in the row.
            ' Change the ColumnIndex to your first combobox index.
            ' DataGridView1(0, e.RowIndex) because the first combobox index is 0 in this sample.
            Dim outputByStation = outputs.Where(Function(x) x.StationName = DataGridView1(0, e.RowIndex).Value.ToString())

            ' Get current cell, we're going to populate the combobox
            Dim currentCell = CType(DataGridView1(e.ColumnIndex, e.RowIndex), DataGridViewComboBoxCell)

            ' Populate the cell's combobox.
            currentCell.Items.Clear()
            For Each output In outputByStation
                currentCell.Items.Add(output.OutputName)
            Next

        End If

    End Sub

End Class

Public Class Station

    Public Property StationName As String

End Class

Public Class Output

    Public Property OutputName() As String
    Public Property StationName() As String

End Class

屏幕截图:

enter image description here

关于vb.net - 在 DatagridView VB.Net 中填充 ComboBox 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38316241/

相关文章:

vb.net - 从父窗体 MenuStrip 中删除 ToolStripMenuItem

asp.net - ServiceController.start() 和 ServiceController.stop() 抛出异常?

resharper - 如何在Resharper中而不是 “Keep Open”模式下正常打开文件

c# - 我们可以只对所有单元测试类使用一个 ClassInitialize 吗?

c# - DataGridView CellValidated 事件在关闭表单时触发

vb.net - VB.NET 中字典初始化行之间的注释

c# - 在 Visual Studio 中添加 VB6 引用会得到 "Type library importer encountered an error during type verification"

c# - 命令行构建解决方案与 Visual Studio 2012 构建

c# - 在 C# 中将 CheckBox 添加到 Datagridview header 并使用更改滚动条更改复选框的位置

c# - 将 datagridviewcolumn 绑定(bind)到嵌套类