Filtering data read from a SQL table based upon user input in a TextBox - vb.net

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 :)

Related

Selected ListView item column value

I have created a listview,
and id like to be able to change the selected item values. I wonder if is possible to somehow access the "ID" string of the selected item.
I created an event to detect when the user selects an item and tried colID.Index.ToString but it just returns the actual column index. Anyone able to provide a syntax that will return the selected item ID?
Private Sub lvwCars_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lvwCars.SelectedIndexChanged
currentCarIDString = colID.Index.ToString
end sub
It's either going to be the item text or subitem text.
currentCarIDString = lvwCars.Items(colID.Index).Text
or
currentCarIDString = lvwCars.Items(colID.Index).SubItems(1).Text
I found something that works for my specific case
ListView.FocusedItem.Index
since my ID is always going by up by one, this solution works perfect. However if anyone can offer a solution that can retrieve the actual value of a row of a column, that would be great.

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

Get the actual value of a BoundField element in a gridview VB.NET

The following image depicts the full gridview I have:
I'm able to get the values of the second column (project_ID) in the back end as follows:
Dim val As String = grdProjects.SelectedRow.Cells(1).Text.ToString
For instance if I press the Select of the third row I'll get back the value 3.
My problem appears when the gridview is updated and has less records as the one in the following image.
If I press the second record I get the value 2 instead of the value 4 that I'd like to get.
Any suggestions please on how to get the actual value of the cell instead of the number of the selected row?
I might be wrong but in VB.net datagrids SelectedRow does not exist, SelectedRows do exist.
Anyway, try this way and let me know if it work:
On the RowEnter or CellClick event use the E.RowIndex to get what you want. Do as follow:
Private Sub DataGrid1_RowEnter(sender As Object, e As DataGridViewCellEventArgs) Handles DataGrid1.RowEnter
Dim CellValue As String = DataGrid1.Rows(e.RowIndex).Cells("Project_ID").Value.ToString
End Sub
Notice that for the .cells() I use the name of the column, this is better because if in the future you add columns this code will not need to be changed. Easier code maintenance always pay! Also. Cell().Value instead Cell().Text
Nandostyle

Datagridview - fill row from Combobox

I have set a combobox to be visible in column1 of my Datagridview. Now I'm trying to fill same row of Datagridview where Combobox appears, from Combobox_Key_Down event. This is my code for showing combobox:
Private Sub My_DGV_CellMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles MY_DGV.CellMouseClick
If e.RowIndex >= 0 Then
With My_DGV
If .Columns(.Rows(e.RowIndex).Cells(e.ColumnIndex).ColumnIndex).Name = "Column1" Then
.CurrentCell = .Rows(.CurrentRow.Index).Cells(.CurrentCell.ColumnIndex)
Show_Combobox(.CurrentRow.Index, .CurrentCell.ColumnIndex) 'function that shows my Combobox in that cells
Combo.Visible = True
Else
Combo.Visible = False
End If
End With
End If
End Sub
I tried many things, but I don't know how to determine in which row Combobox appears and how give that Datagridview row my Combobox values. Someone please give me a clue of what should I do. Thanks in advance !
The first problem with your approach is that the DGV can have only one DataSource: it can either show the m:m association table or the related elements. If you include columns from one of the tables into the query for display, the table becomes non updatable and users can be confused why they cannot edit something they can see. It seems of little value they way you describe it, since they cannot see the detail data until after they make a selection.
Next, it requires another datatable to supply the details for CboColB. Since you want the DGV bound to a DataTable easy updates, you end up having to poke data into cells over and over.
Finally, consider what the user is confronted with. Using a Country table (200+ countries/locales with ISO code and name) and a list of flag colors, a table for CountryFlagColors will have hundreds and hundreds of rows (at just 2 colors per flag).
A better display might be to filter the m:m table (flagcolor) to a selected item so the user is only confronted with the data subset they are currently interested in:
The datatable used in the DGV is built from the m:m table:
The Country column is hidden.
When they choose from the CBO at the top, that is used as a RowFilter to limit the rows to the relevant ones.
In the RowValidating event, when the country cell is DBNull, copy the SelectedValue from the country combo to the DGV cell to fill in the blank
I would probably really make the user click a button and manually add a row so I could seed the country value then rather than depend on events.
It uses a DataAdapter and after adding X number of flag definitions, da.Update(dtFlagColors) applies/saves all the changes.
Ok, so that provides the core functionality to assign N color selections to define the flag colors for a country. The missing element is the 'details' for the Color item.
I added a meaningless int and string item to the Color table, one way to display these would be to create an alias in the SQL with the important details. Displaying them as discrete elements can either make the query non updatable or invites the user to edit things they cannot edit here. My silly SQL:
"SELECT Id, Name, Concat(Name , ' (' , intItem , ' ' , stritem,')') As Info from FColor"
Then use 'Info' as the display member on the CBO column in the dgv:
dc = DirectCast(dgvCF.Columns(0), DataGridViewComboBoxColumn)
dc.DataSource = dtFlagColors
dc.DisplayMember = "info"
dc.ValueMember = "id"
dgvCF.DataSource = dtSample
The combo column has its own datasource of course, in order to display one thing and use another for as the Value to give back to you. Result (the values are silly):
It is not exactly what you want, but comes close and is much simpler. It also requires almost no code for driving the associative entity. Another alternative would be to use a DGV as the second picker so you can show the extended data and manually add rows to a DGV:
If you set the dropdown style to Nothing, it looks like a text column.

Binding combobox selected value to a specific column of current row (not a datagridview)

I have a database that I am using in a VB 2010 project. What I did was removed a textbox for a database field that I dragged onto the form and replaced it with a combobox. The field name was orderstatus.
The problem is this: since I removed the textbox field from the form, I can't seem to bind the combobox value to the field in the active record. In other words, orderstatus textbox doesn't exist anymore. I want to let the user pick a status from the combobox and store that value to orderstatus so that it's saved to the current record my database.
I want to do something like this:
Private Sub Button16_Click_1(sender As System.Object, e As System.EventArgs) Handles Button16.Click
orderstatus = ComboBox13.SelectedValue
Me.OrdersDataSet.orders(0).orderstatus = orderstatus
Me.Validate()
Me.OrdersBindingSource.EndEdit()
Me.TableAdapterManager12.UpdateAll(Me.OrdersDataSet)
End Sub
but it doesn't like my second line where I try to assign the value to the field, saying there is no row 0. All I want to do is put the selected value of the combobox into the orderstatus field of the record being created (or updated).
I've also tried using:
Me.OrdersDataSet.orders.orderstatusColumn = orderstatus
and I get a message saying that the property of the column is ReadOnly. I'm not sure how that's possible because I configured the dataset to update, etc.
I should probably mention that I'm not using a datagridview but a details view, if that makes a difference. I've seen some talk about how to do this using datagridview and don't know if that would work in my case.
What am I doing wrong? What should I use to update just the column I want in the current row?
Ugh, turns out I had forgot to set the databinding properties of the field in question to save directly to the binding source and also select the selecteditem to bind. It was right there and I didn't realize. Ya live and learn. :)