简体   繁体   English

使用VB.net和Excel(组合框和搜索)

[英]Using VB.net and excel (combo box & search)

New to programming and need to make a program in VB to interact with the Excel in the background 编程新手,需要在VB中创建程序才能在后台与Excel交互

The Idea is to have two combo boxes, one "country", the other "city" (If country chosen, list of cities would be reduced to that country; if city is chosen, country would be selected automatically); 这个想法是有两个组合框,一个“国家”,另一个“城市”(如果选择了国家,则城市列表将减少到该国家;如果选择了城市,则将自动选择国家);

I have a DataGridView which displays two columns from the excel speadsheet, the person's fist name and the last name. 我有一个DataGridView,它显示excel speadsheet中的两列,该人的拳头名称和姓氏。

So given the city or a country/city a list of people would be displayed living in that country. 因此,给定一个城市或一个国家/城市,将显示居住在该国家/地区的人员列表。

Also I'm trying to make a searchbox, whereby when you type into a textfield, given what you have typed so far it would filter the first name (containing what you have typed) in the GridView. 另外,我正在尝试创建一个搜索框,当您在文本字段中键入内容时,鉴于您到目前为止所键入的内容,它将过滤GridView中的名字(包含您键入的内容)。

So far I have managed to get other things working, such as radiobuttons and checkboxes with different options. 到目前为止,我设法使其他功能正常工作,例如具有不同选项的单选按钮和复选框。

I could not find anything that would work in regards to the comboboxes and the search field (without a button, so "live" filtering process would need to take place). 对于组合框和搜索字段,我找不到任何有效的方法(没有按钮,因此需要进行“实时”过滤过程)。

I am comfortable with SQL so I've been using sql so far to send queries to the excel and retrieve info for the grid view. 我对SQL很满意,因此到目前为止,我一直在使用sql将查询发送到excel并为网格视图检索信息。

Could you please help? 能否请你帮忙? (may be provide with the template of how I should code it....) (可能提供了我应该如何编码的模板。...)


I am using Visual Basic 2012 and OleDB 4.0 (If i'm not mistaken) to make the connection. 我正在使用Visual Basic 2012和OleDB 4.0(如果我没记错的话)建立连接。


Given the comments below, the workaround that I've found for the search-bar is to create a button, then make it as small as possible and hide it behind the search bar by 'sending it to the back' as making it invisible seemed to have disabled it completely... 鉴于以下评论,我为搜索栏找到的解决方法是创建一个按钮,然后使其尽可能小,然后通过“向后发送”使其隐藏,将其隐藏在搜索栏后面,以使其看起来不可见。完全禁用它...

Then I have used the below code for the 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

And for the text Field (to make the search results come up when the user hits Enter): 对于文本字段(当用户单击Enter时显示搜索结果):

Private Sub CountrySearch_TextChanged(sender As Object, e As EventArgs) Handles CountrySearch.TextChanged

    Me.AcceptButton = BtSearch

End Sub

Next step: I will probably try to apply the filter so that the user wouldn't have to hit enter for the grid view to update 下一步:我可能会尝试应用过滤器,以便用户不必按Enter即可更新网格视图


Any development I will post here, so that as suggested, if anyone else has the same problem, it would be clearer to see follow... 我将在此处发布任何开发内容,以便按照建议进行操作,如果其他任何人也遇到相同的问题,则可以很清楚地看到后续问题...

You can apply Filter to the BindingSource for a DataGridView . 您可以将Filter应用于DataGridViewBindingSource You can put that code in the event handler for a ComboBox 's SelectedIndexChanged event: 您可以将该代码放入ComboBoxSelectedIndexChanged事件的事件处理程序中:

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

There are other handlers for ComboBox that might be more applicable, like SelectedValueChanged or TextChanged . 还有其他适用于ComboBox处理程序,例如SelectedValueChangedTextChanged
There are also other values that may be more applicable, depending how the ComboBox is being populated, such as .Text or .SelectedText . 根据ComboBox的填充方式,还有其他一些值可能更适用,例如.Text.SelectedText

Okay Basically I've done the search as described in the question. 好的,基本上我已经按照问题中的描述进行了搜索。

Regarding the combo boxes I've left one combo box. 关于组合框,我留下了一个组合框。

Populated it through the GUI/Designer Mode: 通过GUI /设计器模式填充它:

  • Select the combo box 选择组合框

  • Click on the small arrow 点击小箭头

  • Click on Edit Items 点击编辑项目

  • A window will pop-up - enter your options there (one option per line) 将会弹出一个窗口-在此输入您的选项(每行一个选项)

  • Once you've done that, save it 完成后,将其保存

Double click on it and code-mode should come up 双击它,代码模式应该出现

I had a FillSearchResults Function which would send the SQL Statements to the excel and put that info into the grid view: 我有一个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

Now to link the country fields: 现在链接国家字段:

    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

And Done :) 并做了 :)

Whenever a country is selected, only results for that country would show 每当选择一个国家/地区时,只会显示该国家/地区的结果

PS: I called the country combo box as "Country"... PS:我称国家组合框为“国家” ...

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

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