2016-07-11 44 views
1

我有一個datagridview有2列作爲組合框,我想填充第二個取決於第一個。在DatagridView中填充組合框列VB.Net

Ex。我在我的站

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 [email protected]"    
        .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事件下的代碼IM。

+1

這聽起來像您使用的是相同的實例作爲數據源爲他們。很難說沒有代碼。 – Plutonix

+0

我添加了即時消息使用的代碼 –

+0

恐怕我所認爲的工作實際上沒有。我確信它可以完成,但我現在沒有時間去詳細說明細節,因此我刪除了不完整的答案。 – jmcilhinney

回答

0

由於無法設置列的數據源,所以這有點棘手。設置列的數據源會影響整個列。您必須分別設置每個單元格的數據源。我會告訴你如何去做。

首先添加一個空格式的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

+0

這正是我正在尋找的非常感謝你投票bcuz我沒有15聲望 –

+0

你可以接受我的答案。你獲得接受答案的聲望點。我看到你問了一些問題,但從不接受任何答案。請接受解決您問題的答案。 – Han

+0

當你有足夠的聲望來點贊時,請提出你的問題的好答案。 – Han