使用VB.net和Excel(combobox和search)

编程新手,需要在VB中使程序与后台的Excel进行交互

想法是有两个combobox,一个“国家”,另一个“城市”(如果select的国家,城市名单将减less到该国;如果select城市,则自动select国家);

我有一个DataGridView,它显示了两个来自excel speadsheet,人的拳头名称和姓氏的列。

所以考虑到这个城市或者一个国家/城市,一个人的名单将被展示在那个国家。

此外,我正在努力build立一个search框,当你键入一个文本字段时,给定你到目前为止input的内容,它将过滤GridView中的名字(包含你input的内容)。

到目前为止,我已经设法得到其他的工作,如单选button和checkbox不同的选项。

我找不到任何可以用于combobox和search字段的工具(没有button,所以需要进行“实时”过滤过程)。

我很熟悉SQL,因此我一直在使用sql向excel发送查询并检索网格视图的信息。

能否请你帮忙? (可能会提供我如何编码的模板….)


我正在使用Visual Basic 2012和OleDB 4.0(如果我没有错)进行连接。


鉴于下面的评论,我find的search栏的解决方法是创build一个button,然后使其尽可能小,并隐藏在search栏后面“发送到后面”,使其看起来似乎完全禁用它…

然后,我已经使用下面的代码为button:

Private Sub BtSearch_Click(sender As Object, e As EventArgs) Handles BtSearch.Click Try FillSearchResults("SELECT First_Name, Last_Name FROM [Database$] WHERE Country LIKE '%" & Country.Text & "%'") Country.Text = dt.Rows(0).Item(1) Catch ex As Exception MsgBox("Not Found") End Try End Sub 

对于文本字段(使用户点击Enter时search结果):

 Private Sub CountrySearch_TextChanged(sender As Object, e As EventArgs) Handles CountrySearch.TextChanged Me.AcceptButton = BtSearch End Sub 

下一步:我可能会尝试应用filter,以便用户不必击中input网格视图来更新


任何发展,我会张贴在这里,所以如build议,如果任何其他人有同样的问题,会更清晰地看到后续…

您可以将Filter应用于DataGridViewBindingSource 。 您可以将该代码放入ComboBoxSelectedIndexChanged事件的事件处理程序中:

 Private Sub myComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles myComboBox.SelectedIndexChanged bindingSourceForMyDataGridView.Filter = "SomeColumn LIKE '%" & myComboBox.SelectedValue & "%'" End Sub 

ComboBox还有其他的处理程序可能更适用,如SelectedValueChangedTextChanged
还有其他值可能更适用,取决于如何填充ComboBox ,如.Text.SelectedText

好的基本上我已经完成了问题中所描述的search。

关于combobox我留下了一个combobox。

通过GUI / Designer模式填充它:

  • selectcombobox

  • 点击小箭头

  • 点击编辑项目

  • 一个窗口会popup – 在那里input你的选项(每行一个选项)

  • 一旦你完成了,保存它

双击它,代码模式应该出现

我有一个FillSearchResults函数,它将SQL语句发送到Excel,并将这些信息放入网格视图中:

  Private Sub FillSearchResults(ByVal Query As String) Dim da As OleDbDataAdapter Dim dt As New DataTable 'The Connection should already be open' da = New OleDbDataAdapter(Query, cn) dt.Clear() da.Fill(dt) 'To name the columns and make it so that when the window size is changed the column widths would automatically adjust' With SearchResults .DataSource = dt .Columns(0).HeaderText = "First Name" .Columns(1).HeaderText = "Second Name" .Columns(2).HeaderText = "Country" .Columns(3).HeaderText = "Completed Fully" .Columns(0).Width = 70 End With 'To Color the whole rows depending if all of the information is complete (I do realise this is not the best way to do it but still...)' For i As Integer = 0 To Me.SearchResults.Rows.Count - 1 If Me.SearchResults.Rows(i).Cells("Completed_Fully").Value.ToString = "No" Then Me.SearchResults.Rows(i).Cells("First_Name").Style.BackColor = Color.Cornsilk Me.SearchResults.Rows(i).Cells("Second_Name").Style.BackColor = Color.Cornsilk Me.SearchResults.Rows(i).Cells("Country").Style.BackColor = Color.Cornsilk Me.SearchResults.Rows(i).Cells("Completed_Fully").Style.BackColor = Color.Cornsilk ElseIf Me.SearchResults.Rows(i).Cells("Completed_Fully").Value.ToString = "Yes" Then Me.SearchResults.Rows(i).Cells("First_Name").Style.BackColor = Color.Azure Me.SearchResults.Rows(i).Cells("Second_Name").Style.BackColor = Color.Azure Me.SearchResults.Rows(i).Cells("Country").Style.BackColor = Color.Azure Me.SearchResults.Rows(i).Cells("Completed_Fully").Style.BackColor = Color.Azure Else Me.SearchResults.RowsDefaultCellStyle.BackColor = Color.Red Me.SearchResults.ClearSelection() End If Next 'To Clear the first row/cell being selected' Me.SearchResults.ClearSelection() End Sub 

现在链接国家字段:

  Private Sub Country_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Country.SelectedIndexChanged FillSearchResults(SELECT Country FROM Database WHERE Country LIKE '%" & Country.Text & "%' ") End Sub 

并做了 :)

每当select一个国家,只有该国的结果才会显示

PS:我把国家combobox称为“国家”…