简体   繁体   English

在DatagridView VB.Net中填充ComboBox列

[英]Filling ComboBox Column in DatagridView VB.Net

i have a datagridview with 2 columns as combobox and i want to fill the second one depending on the first one. 我有一个带有2列的数据网格视图作为组合框,我想根据第一个填充第二个。

Ex. 防爆。 I have a table in my database with stations 我的数据库中有一个带工作站的表

TableStations
Station 1 
Station 2

And each stations has a different amount of outputs 每个站点的输出量都不同

Ex. 防爆。

Station 1      Station 2
OutP1            OutP5
OutP2            OutP6
                 OutP7

What i want to do in the datagridview is that when the user selects from the first combobox a station the next combobox gets filled with the outputs for that station, my problem comes when the user adds a second row in the datagridview if he selects a diferent station the info in the first row will be modified. 我想在datagridview中做的是,当用户从第一个组合框中选择一个工作站,下一个组合框充满了该工作站的输出时,如果用户在datagridview中添加第二行,如果他选择了不同的,我的问题就来了站点将修改第一行中的信息。

Is there any solution for this or any other way to do what i want? 有没有任何解决方案可以用这个或任何其他方式来做我想要的?

Thanks in advance 提前致谢

EDIT: this is the code im using 编辑:这是我正在使用的代码

                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()

This code is under the cellclick event of my datagridview. 此代码位于datagridview的cellclick事件下。

This is a bit tricky since you can't set the column's data source. 这有点棘手,因为您无法设置列的数据源。 Setting the column's data source affects the entire column. 设置列的数据源会影响整个列。 You must set the data source of each cell separately. 您必须分别设置每个单元格的数据源。 I'll show you how to do it. 我会告诉你如何做到这一点。

First add a DataGridView in an empty form. 首先在空表单中添加DataGridView。 Don't add the columns, we're going to add the columns by code. 不要添加列,我们将按代码添加列。 You don't have to add the columns by code in your real project, but please follow what I did in this example. 您不必在实际项目中按代码添加列,但请按照我在此示例中所做的操作。 I add comments to make the code easy to understand. 我添加注释以使代码易于理解。 I choose to create two classes to hold Station and Output. 我选择创建两个类来保存Station和Output。 This is also optional, you can just use a DataReader and add them manually. 这也是可选的,您可以使用DataReader并手动添加它们。 Hope this helps you. 希望这对你有所帮助。

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

Screenshot: 截图:

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM