2
votes

i have a datagridview with 2 columns as combobox and i want to fill the second one depending on the first one.

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.

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.

1
It sounds like you are using the same instance as the datasource for them. Hard to tell without code.Ňɏssa Pøngjǣrdenlarp
i added the code that im usingCode_Geass
I'm afraid that what I thought would work actually did not. I'm sure that it can be done but I don't have time to work out the details right now so I have removed my incomplete answer.jmcilhinney

1 Answers

2
votes

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. 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. This is also optional, you can just use a DataReader and add them manually. 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:

enter image description here