Visual Basic 2008 Mobile Project: How to Filter Data From Combo Box to DataGrid - vb.net

I'm working on a mobile project, and I have this:
A search textbox (fillby method)
A combobox (bound to the data)
A datagrid
I am able to do this:
input a search query into the textbox using the fillby method and the datagrid shows the appropriate rows.
I need help with this:
To filter the same data with a combobox. If I use the Add Query method (fillby method) to a combobox it creates another textbox search query. I don't want that. I want to be able to filter the datagrid by the combobox.
Here is my code for the ComboBox Sub:
Private Sub CityComboBox_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CityComboBox.SelectedValueChanged
Dim RestIDShort As Short 'primary key
Dim RestDataRow As DataRow 'complete data row of selected value
Dim RestDataRows As DataRow() 'holding the data
Try
'get the restID for the selected city
RestIDShort = Convert.ToInt16(CityComboBox.SelectedValue)
'find the row from the table for the selected city
RestDataRow = RestaurantEateriesDataSet.RestaurantTable.FindByRestID(RestIDShort)
'Grab the variables here. Don't really need them. Just to see if I can pull data.
'NameStringShow = RestDataRow("Name")
'FoodTypeStringShow = RestDataRow("FoodCat")
'CityStringShow = RestDataRow("City")
'test to see if we can write to screen
'successfully wrote this to the screen onload but not on combobox change
'TextBox1.Text = NameStringShow
'retrieve the array for the selected data row
'not sure if this is how to call when there is only one table????
RestDataRows = RestDataRow.GetChildRows("RestaurantTable")
'fill the datagrid with the array of selected value rows
'I don't know how to do this part:
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
I do have a query created that I can call (if needed). The query works when I call it in a textbox, so if there is a way to call it in a combo box, and then display the selected fields in the datagrid . . . all would be good.
Any help, much appreciated.

Private Sub bttnFilter Click(...) Handles bttnFilter.Click
Dim filter As String
filter = InputBox("Enter product name, or part of it")
ProductsBindingSource.Filter = "ProductName LIKE '%" & filter.Trim & "%'"
End Sub
the same is applied to combobox, use combobox1.selectedItem() in place of the filter.
its the basics of searching, it you have any other question you're welcome.

Related

Pop up window with textbox after selecting checkbox

all
I am working on a project with vb.net and MySQL database.
Now for taking information I have added few checkboxes, and in case if user selects a checkbox named other I want a window to appear and it should have a text box and when user enters the text at that box, the details should be stored in db.
As you haven't provided much information I may can't answer exactly your question but check if this can help.
First Add LINQ to SQL (.dbml File) - Let's name it as XYZ and Dataset (.xsd File) - let's name this as XYZ too, to your project and then drag and drop your database table in both the files and save all.
Now going into your form which contains the check box you mentioned.
Add this code to you checkbox click_event.
If checkbox1.checked = True Then
Dim insertValue As String = ""
insertValue = InputBox("Enter text to insert", YourTitle, "")
If inserValue <> "" Then
Dim db as New XYZDataContext
Dim NewRec As New YourTableName With {.ColumnName = insertValue}
db.YourTableName.InsertOnSubmit(NewRec)
db.SubmitChanges()
Msgbox("Value added!")
End If
checkbox1.checked = False
End If
create your window in the designer and give it a name like someWindow. then in your code open the window from the click event of your checkbox. when you close the window don't dispose it. just hide it me.hide so when you window closes you can retrieve the data from your textbox.
Sub Test ()
Dim wd_SomeInfo as new someWindow
wd_SomeInfo.showdialog()
Dim result As String = wd_SomeInfo.txt_sometextbox.text
If result = "" Then cancel....
End Sub

Verify multiple comboboxes/textboxes have value

I have a winform that has about 6 comboboxes and 3 textboxes. I also have a button that takes the values of aforementioned controls and inserts them into SQL. Is there a better way, than a bunch of nested if/then statements, to verify that the controls all have values before inserting the data? This gets ugly very fast. I have tried googling this answer, but I get too many ASPx answers. I will entertain any ideas you may have. I am just trying to find a better way to do this. Thanks for any help.
I think its not too hard.
This code shows error message about the first empty textbox or combobox. But you can easily modify to show error message for all empty comboboxes and textboxes if required.
Private Function ValidateMyControls() As Boolean
Dim allComboBoxes() As ComboBox = {ComboBox1, ComboBox2, ComboBox3} ' add all your comboboxes here
Dim allTextBoxes() As TextBox = {TextBox1, TextBox2, TextBox3} ' add all your textboxes here
Dim emptyTB As TextBox = allTextBoxes.Where(Function(f) f.Text = "").FirstOrDefault
Dim emptyCB As ComboBox = allComboBoxes.Where(Function(f) f.SelectedIndex = -1).FirstOrDefault
If emptyTB IsNot Nothing Then
MessageBox.Show("Please fill value in " & emptyTB.Name)
Return False
ElseIf emptyCB IsNot Nothing Then
MessageBox.Show("Please select a value in dropdown " & emptyCB.Name)
Return False
Else
' All set to go!
Return True
End If
End Function

Limiting my result set in VB

So I can fill the combobox I have going in Visual Studio just how I want with ALL results with the following:
Dim pnum As New List(Of String)
For Each polnumber As InsuredDataSet.Claims_InsuredRow In Me.InsuredDataSet.Claims_Insured
pnum.Add(polnumber.Policy_Number)
Next
pnum.Reverse()
Me.Policy_NumberComboBox.DataSource = pnum
Awesome. Now I want to limit the pnum by taking what was input/selected from Insured_NameTextBox on the form and only returning the Policy_Number with a matching Insured_Name. I figure this can be performed with an If statement, but everything I try (stringcompare, InsuredName_TextBox = Me.InsuredDataSet.ClaimsInsured, etc.) either doesn't limit the results OR limits the results entirely so nothing shows up. Any idea where to put the If statement and what should be compared?
UPDATE:
I think there is some confusion so I'm including the entire load sub below:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'IncidentsDataSet.Claims_Incidents' table. You can move, or remove it, as needed.
Me.Claims_IncidentsTableAdapter.Fill(Me.IncidentsDataSet.Claims_Incidents)
'TODO: This line of code loads data into the 'InsuredDataSet.Claims_Insured' table. You can move, or remove it, as needed.
Me.Claims_InsuredTableAdapter.Fill(Me.InsuredDataSet.Claims_Insured)
'textbox autocomplete mode
Dim Iname As New AutoCompleteStringCollection()
For Each insname As InsuredDataSet.Claims_InsuredRow In Me.InsuredDataSet.Claims_Insured
Iname.Add(insname.Insured_Name)
Next
Me.Insured_NameTextBox.AutoCompleteCustomSource = Iname
'combobox autocomplete code (now sorting by last included!)
Dim pnum As New List(Of String)
For Each polnumber As InsuredDataSet.Claims_InsuredRow In Me.InsuredDataSet.Claims_Insured
pnum.Add(polnumber.Policy_Number)
Next
pnum.Reverse()
Me.Policy_NumberComboBox.DataSource = pnum
End Sub
Try something like this:
Me.Policy_NumberComboBox.DataSource = InsuredDataSet.Claims_Insured.Where(Function(r) r.Insured_Name = Insured_NameTextBox.Text).Select(Function(r) r.Policy_Number).Reverse()
We're getting closer. Based on the update to your question, you're running this code when the form loads. However, at the point where the form loads, your textbox will always be empty. What do you do when the value in the textbox changes, to re-filter your data?
This is C#
Me.InsuredDataSet.Claims_Insured.Where(x => x.Insured_Name == Insured_NameTextBox.Text);

vb.net - click on listview subitem and open window

I'm not sure if this is possible after doing a bunch of googling, but hopefully it is. I have an application that pulls a list of information from a MySQL database and populates a listview (Unfortunately, I can't change to a datagrid at this time.) What I'm tasked to do is make it so that when clicking on a certain column, a window will open and, based on the ID of the row that was clicked on, retrieve another set of results from the same database.
The list view is created as such:
Do While result.Read()
Dim siteid = (result.Item("idsite").ToString())
Dim sitename = (result.Item("name").ToString())
Dim last_import_date = (result.Item("import_finished").ToString())
Dim last_import_file = (result.Item("file_name").ToString())
Dim last_line = (result.Item("last_line").ToString())
Dim status = (result.Item("status").ToString())
Dim lv As ListViewItem = ListView1.Items.Add(siteid)
lv.SubItems.Add(sitename)
lv.SubItems.Add(last_import_date)
lv.SubItems.Add(last_import_file)
lv.SubItems.Add(last_line)
lv.SubItems.Add(status)
Loop
So preferably I'd like to click on "Last_import_file" and have that open the window. I've tried a bunch of ItemClicked type commands, but haven't had much luck.
Is what I'm attempting possible? I don't need any special text formatting, just want to register the click and pop open the dialog.
Thanks!
Yes it is possible. To do this in a Listview it is a bit more complicated than a DataGridView. You'll need to make use of the ListViewHitTestInfo class. Using the MouseDown Event of your listview, use this code:
Private Sub ListView1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDown
Dim info As ListViewHitTestInfo = ListView1.HitTest(e.X, e.Y)
MsgBox(info.Location.ToString())
If Not IsNothing(info.SubItem) Then
'info will contain the information of the clicked listview column. You can then go through it's subitems for more information, if any.
End If
End Sub

How to change the data source of a ComboBox?

I have two comboboxes. The data sorce of combobox1 is a list of string that is fixed. Combobox2's data sorce will be a list of string that depends on the selection of combobox1. This is very similar to a common case as follows: first enter your contry, then depending on your enter, the second combobox will show you the list of universities in the country.
'Change selection of first combobox
Private Sub cbxClient_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbxClient.SelectedIndexChanged
Try
If cbxClient.SelectedIndex <> -1 Then
GetAccount()
End If
Catch
Throw
End Try
End Sub
'Based on selection of first combobox, update the data sorce of second combobox
Private Sub GetAccount()
Try
m_listAccount.Clear()
Dim strClient As String = cbxClient.SelectedItem.ToString
For i As Integer = 0 To m_listDS.Count - 1
If m_listDS(i).Client.Tostring = strClient Then
m_ds = m_listDS(i)
Exit For
End If
Next
If Not m_ds Is Nothing Then
For Each row As DataRow In m_ds.Tables("Account").Rows
m_listAccount.Add(row("account").ToString)
Next
End If
cbxAccount.DataSource = m_listAccount
Catch ex As Exception
End Try
End Sub
My problem is that though m_listAccount updates (has the correct information), the choices shown in cbxAccount are not updated according to m_listAccount (has the wrong information). I do not understand why this happens.
Note: Assume that old string in m_listAccount is {"old1"} (the list only has 1 string) and after updating, the string in m_listAccount is {"new"}. Through break points, I get the following:
cbxAccount.DataSorce={"new"}
cbxAccount.SelectedItem={"old1"}
In the form, cbxAccount shows "old1" string.
Try this,
cbxAccount.DataSource = Nothing
cbxAccount.DataSource = m_listAccount
If you are just updating the same list object, the datasource might not automatically update, but should if the pointer changes to the datasource.
Or you could try,
m_listAccount.AcceptChanges()
If m_listAccount is a DataTable
Doesn't look like you're actually calling the databind method, i.e.
cbxAccount.DataBind()
Try putting that just after you set the data source.