Search data inside Datagridview without database - VB.net - vb.net

I've searched around and couldn't find the one I've been looking for.
I have a DataGridView in VB.NET with thousands of records from MySQL Database. Now, I want to SEARCH item_description that matches or something LIKE the inputted text. I am using TEXTCHANGED
I didn't use search to database since its taking time to load.
Sample is
Search: orange
DataGridView must filter and display only with the words like "orange"
ITEM_ID
ITEM_DESCRIPTION
120
Orange Juice
832
Orange Fruit
I am thinking of getting the array list names of the column "item_description" and filter it but I don't know where to start and what codes to use. Thank you

Assuming that you have taken the advice in my comment and populated a DataTable and then bound that to the DataGridView via a BindingSource, filtering the data is a simple matter of setting the Filter property of that BindingSource. In your case, it should be something like this:
myBindingSource.Filter = "ITEM_DESCRIPTION LIKE 'orange*'"
One issue with filtering on the TextChanged event of a TextBox is that you will filter multiple times when the user types multiple characters when you only need the last one. To alleviate that, I suggest using a Timer that you start/restart each time a character is typed and then filter when it Ticks. That will enable the user to type multiple characters without filtering but also not have a significant wait after they stop typing for the filtering to be done. I recommend an interval of around 250 milliseconds but you can experiment to see what you think is best. E.g.
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
'Start/restart the Timer.
Timer1.Stop()
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Timer1.Stop()
'Perform the filtering.
BindingSource1.Filter = $"ColumnName LIKE '{TextBox1.Text}*'"
End Sub

Related

How to Fill ComboBox with DataTable Column Names in VB.NET?

I need to fill a combobox on a form with the column names from an existing data table. I don't want to manually enter the column names in case they change in the future. I also want to keep my code tidy, so I don't want a standard loop. How can I fill a combobox with the column names most efficiently?
Thanks to examples from here, here, and here, I was able to piece together the following LINQ query and set it directly to the data source on the combobox when the form loads.
Private Sub Form_Load(sender As Object, e As EventArgs) Handles Me.Load
comboBox.DataSource = (From dc In dataTable.Columns
Select dc.ColumnName).ToArray()
End Sub

Saving Multiple Times of Day in TextBox - Visual Basic

I'm having trouble figuring out how to save the time of day to a text box. The time of day is generated on a click, and I would like it to add it to the list every time I click, while keeping the older values in the list. I know how to make a list from list a list box, but I need to copy and paste the times from the box into excel. (And if I haven't asked enough already, how to format those times to be used in Excel)
To generate the time, I'm using
Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Timer1.Start()
TextBox1.Text = Date.Now.ToString("hh:mm:ss")
You can use a ListBox
On button click event do
ListBox1.Items.Add(DateTime.Now.ToString()
But it you have to use text box
Then use ' +=' operator
TextBox1.text += DateTime.Now.ToString()

winforms vb 2008 textbox validation issue

I have vb 2008 Winforms application.
In my Branch form I have 11 textbox fields and I want to validate 8 of them, either mandatory, or required format such as UK postcode, UK tel nos etc.
My issue now is that when the validation starts, it is validating the last text field first ( or seems to be)
here is my code
For Each oCtrl As Control In Me.Controls
If TypeOf oCtrl Is TextBox Then
oCtrl.Focus()
If Validate() = False Then
Exit Sub
End If
End If
Next
what's wrong please?
what's wrong please?
The controls collection isn't sorted or grouped. Your loop will access them in whatever order the collection has them.
Without more code it's hard to say how to fix it. However a tip may be in order. Use the same handler to handle the validate event for each textbox. This way you can keep the user at that textbox until the input is valid.
is it possible to add the items to the collection in the order of their tab indexes on form Shown event, how would I do that please?
A List(Of TextBox) and a custom sorter would probably be the way to go
Dim AllTB As New List(Of TextBox)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
AllTB.AddRange(Me.Controls.OfType(Of TextBox))
AllTB.Sort(Function(x, y) x.TabIndex.CompareTo(y.TabIndex))
End Sub
To loop through the textboxes use:
For Each tb As TextBox in AllTB
Because the TextBoxes are in the list by reference you can get or set any of the properties in the textboxes and any changes, will be reflected on your form. You could also use sequential names for the textboxes, tag property, etc. and sort by that.

Best Practice For Error Checking Controls On A Form

So I have a form with a variety of different controls (combobox, textboxes, listboxes, etc).
My first thought is to create a If, Else, End If statement. Well while that would work, it could also get pretty long, depending on the amount of controls and combinations.
Validation could include if a listbox is filled, checkbox is checked, etc pertaining to WinForms.
Is there a better solution to check all possiblities than an If statement?
It might be worthwhile to do the error checking as the user fills out the form. This could be implemented with the LostFocus event. Ex:
Private Sub btnTest_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles yourbutton.LostFocus
Dim txt = yourbutton.Text
If txt = "yourtest" Then
'do stuff
EndIf
End Sub
As above. It depends on the Validation you are trying to do. Are you validating user input, datatype lenght range, etc. Are you validating business rules. Should such and such a value equal something else. There's all kinds of possibilities.

Datagridview not saving changes to DB when Navigator save changes clicked

This is a VB.NET, Winforms App, Using EF. On my form I have a datagridview, a databinding source, and a bindingNavigator... I can edit the cells of the datagridview but when I click save changes the value is only saved until I reload the form.?.? Looking at the database table directly I can see the value never actually changes.. Below is the sub that handles the click..
Private Sub UnitBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UnitBindingNavigatorSaveItem.Click
UnitDataGridView.CommitEdit(DataGridViewDataErrorContexts.CurrentCellChange)
db.SaveChanges()
End Sub
From what I have read this seems like all I need to have but obviously its wrong somehow..
I made a work around for it for the time being... I simply use the CellEndEdit event and get the row info from that. Next I get the value of the column that holds the id and update the database from there. Seems like a long way around but is the only way I can get it to write any data at the moment...