Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在DatagridView VB.Net中填充组合框列_Vb.net_Visual Studio 2012_Datagridview - Fatal编程技术网

在DatagridView VB.Net中填充组合框列

在DatagridView VB.Net中填充组合框列,vb.net,visual-studio-2012,datagridview,Vb.net,Visual Studio 2012,Datagridview,我有一个datagridview,有两列作为组合框,我想根据第一列填充第二列 我的数据库里有一张表,上面有站 TableStations Station 1 Station 2 每个站都有不同数量的输出 前 我想在datagridview中做的是,当用户从第一个组合框中选择一个站点时,下一个组合框将填充该站点的输出,当用户在datagridview中添加第二行时,如果他选择了不同的站点,则第一行中的信息将被修改 有没有解决这个问题的方法或者其他方法来满足我的需求 提前谢谢 编辑:这是我使用的

我有一个datagridview,有两列作为组合框,我想根据第一列填充第二列

我的数据库里有一张表,上面有站

TableStations
Station 1 
Station 2
每个站都有不同数量的输出

我想在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()

此代码位于my 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
屏幕截图:


听起来您使用的实例与它们的数据源相同。没有代码很难说。我添加了我使用的代码。我担心我认为有效的东西实际上没有用。我相信这是可以做到的,但是我现在没有时间去解决细节,所以我删除了我不完整的答案。这正是我想要的。谢谢你,非常不能投票,因为我没有15个好名声,你可以接受我的答案。你接受答案会获得声誉积分。我看到你问了一些问题,但从不接受任何答案。请接受能解决您问题的答案。当您有足够的声誉来投票时,请投票给您的问题好的答案。我如何分别处理每个选择的更改?@Decoder94我建议您提出一个新问题,详细说明您正在尝试做什么。
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