[英]VB.NET (Excel Two Columns > Conditional Comboboxes)
我正在搜索將近兩個小時,以找到以下解決方案。 在Excel中,我有兩列(主記錄為一列,從記錄為一列)。 基本上,我想在Combobox1中填充所有主記錄。 如果選擇了MasterRecord A,我希望Combobox2僅顯示屬於A的SlaveRecords,而不顯示屬於其他Master Record的其他記錄。
我添加了Interop程序集並打開了Excel(已經存在連接)。 非常感謝您的幫助!
Private Sub Combobox2_Populate()
'Start Excel Script to populate ComboBox2
Dim excel As Application = New Application
Dim w As Workbook = excel.Workbooks.Open(Filename:=databasestatus, [ReadOnly]:=True)
Dim sheet As Worksheet = w.Sheets("AIR_NL_1")
Dim StartRow As Integer
Dim TotalRows As Integer
ComboBox2.Items.Clear()
sheet.UsedRange.AutoFilter(Field:=9, Criteria1:=ComboBox1.SelectedItem, Operator:=XlAutoFilterOperator.xlFilterValues)
TotalRows = sheet.Range("A1").CurrentRegion.Rows.Count
For StartRow = 3 To TotalRows
If XlCellType.xlCellTypeVisible = True Then
ComboBox2.Items.Add(sheet.Range("H:H").Cells(StartRow, 1).Text)
End If
Next
w.Close(SaveChanges:=False)
End Sub
這可能會幫助您,或者至少給您一個基本的想法:
Private Function ExcelToDataTable(ByVal fileExcel As String, _
Optional ByVal columnToExtract As String = "*", _
) As System.Data.DataTable
Dim dt As New System.Data.DataTable
Try
Dim MyConnection As System.Data.OleDb.OleDbConnection
Dim MyCommand As OleDbDataAdapter
Dim fileExcelType As String
'Chose the right provider
If IO.Path.GetExtension(fileExcel.ToUpper) = ".XLS" Then
fileExcelType = "Excel 8.0"
MyConnection = _
New System.Data.OleDb.OleDbConnection _
("provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & fileExcel & "';Extended Properties=" & fileExcelType & ";")
Else
fileExcelType = "Excel 12.0"
MyConnection = _
New System.Data.OleDb.OleDbConnection _
("provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & fileExcel & "';Extended Properties=" & fileExcelType & ";")
End If
'Open excel connection
MyConnection.Open()
'Populate DataTable
Dim myTableName = MyConnection.GetSchema("Tables").Rows(0)("TABLE_NAME")
MyCommand = New OleDbDataAdapter(String.Format("SELECT " & columnToExtract & " FROM [{0}] ", myTableName), MyConnection)
MyCommand.TableMappings.Add("Table", columnToExtract)
MyCommand.Fill(dt)
MyConnection.Close()
Catch ex As Exception
Err.Clear()
End Try
Return dt
End Function
如您所見,我們有一個名為myWhereStatement的可選參數。
這是什么意思? 您可以在調用函數時指定其值,否則其值為empty string
之后,我們可以在Sub內部調用ExcelToDataTable
,以填充ComboBox
,如下所示:
Private Sub Combobox_Populate()
Dim filePath As String = "your_file_path"
ComboBox1.DataSource = ExcelToDataTable(filePath, "MasterRecord")
End Sub
現在,您的ComboBox1充滿了數據,但是ComboBox2仍然為空。
我們將處理ComboBox1_SelectedValueChanged
事件,這意味着每次您從ComboBox1
選擇一個項目時,它將以編程方式用適當的項目填充ComboBox2
,如下所示。
Private Sub ComboBox1_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedValueChanged
Dim slaveRecords As System.Data.DataTable = ExcelToDataTable(filePath)
Dim dt As New DataTable
dt.Columns.Add("SlaveRecords")
For i As Integer = 0 To slaveRecords.Rows.Count
If ComboBox1.SelectedItem Is slaveRecords.Rows(i).Item(0) Then
dt.Rows.Add(slaveRecords.Rows(i).Item(1))
End If
Next
ComboBox2.DataSource = dt
End Sub
備注
如您所見,第一次調用ExcelToDataTable
僅具有2個參數,而第二次調用具有3個參數。 那是可選的參數功能!
NB
如您所見,由於更好的代碼格式,我使用了許多_
。 這意味着一條語句將跨多行繼續
如果不是100%清楚的事情,您可以在下面的評論中隨意提出疑問。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.