How to Fill ComboBox with DataTable Column Names in VB.NET? - 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

Related

Active filtering datagridview with texbox when datagridview has a checkbox column

Ιn VB.NET i have a datagridview which has as datasource a datatable.
In order to achieve active filtering, i have a textbox with the follwing code on TextChanged event.
Private Sub KryptonTextBox1_TextChanged(sender As Object, e As EventArgs) Handles KryptonTextBox1.TextChanged, KryptonTextBox5.TextChanged
DirectCast(KryptonDataGridView1.DataSource, DataTable).DefaultView.RowFilter = String.Format("userid like '%{0}%' or name like '%{0}%'", KryptonTextBox1.Text)
End Sub
When i decided to use a checkbox type column , i saw that when a user has made some checks and then he filters, the checks are lost beacuse the datatable is reloaded ...
Any ideas ? (except storing the checked values in the database because i have no rights to do this in the specific case)

Search data inside Datagridview without database - 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

Sorting of Data grid view which is bound table

I want my first column sort of data grid view when form load. but i have no code. Below i attach my loding code.
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'EmployeeTBDataSet.Table1' table. You can move, or remove it, as needed.
Me.Table1TableAdapter.Fill(Me.EmployeeTBDataSet.Table1)
End Sub
You have presumably dragged a table from the Data Sources window onto your form to create that DataGridView and the code that populates the DataTable it is bound to. That will also have created a BindingSource and that is where you perform most actions related to the bound data, including sorting. After that call to Fill, set the Sort property of that BindingSource to the name of the column you want to sort by, e.g.
Me.Table1BindingSource.Sort = "Table1ID"
That will sort in ascending order implicitly. You can also sort in ascending or descending order explicitly, e.g.
Me.Table1BindingSource.Sort = "Table1ID DESC"

Filtering data read from a SQL table based upon user input in a TextBox

I have a table on a SQL Server that will be accessed by different users. Each user will be able add and delete rows in the table on the server.
Currently, the form displays the entire contents of the table and all of the rows are editable. I am trying to allow users to input words or letters into a TextBox at the top of the form to filter the rows which are displayed on the form.
1) I have an event like this:
Public Event ValuesChanged(sender As Object, e As EventArgs)
And a method like this:
Private Sub SearchTxtBox_TextChanged(sender As Object, e As EventArgs)_
Handles SearchTxtBox.TextChanged
2) I need to filter based on the user-input from the Textbox.Text.
I would like the filter to be based on each letter entered into the text box. So, for instance, when you type the letter "A", it would redraw and filter to only show rows that have an "A" or "a". When you type "Ape", it would do the same by narrowing it further.
Ok I think I've got it figured out:
Dim dataview As DataView = _ds.ProgramOwners.DefaultViewdataview.RowFilter = String.Format("Program like '%{0}%'", SearchTxtBox.Text)
This filters based on the Program Row and the user input :)

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.