Winforms AutoComplete Texbox Problem - winforms

Look this
http://csharpdotnetfreak.blogspot.com/2009/01/winforms-autocomplete-textbox-using-c.html
i have applied this for City Field textbox that displays the CityNames on Typing as Suggestion
I have taken a DataTable as DataSource which was pre-filled from Database.
I loop through DataTable and Add a CityNames to the AutoCompleteStringCollection
Now Problem is that How do i get Values that is(CityId) when Save My Record to the Database.

You can use Select method on DataTable to get rows matching the Textbox.Text
DT.Select("Name='" & TextBox1.Text & "'")

Related

ComboBox blank text with SQL Server

I have here the code of my combobox in VB.net which is connected to my SQL Server 2012.
Public Sub LoadCBXStocks(Optional QueryForCBXStocks As String = "")
If QueryForCBXStocks = "" Then
SQL.ExecQuery("SELECT DISTINCT Category FROM tblProduct ORDER BY Category ASC;")
Else
SQL.ExecQuery(QueryForCBXStocks)
End If
If SQL.HasException(True) Then
Exit Sub
cbxStocksCategory.Items.Clear()
For Each r As DataRow In SQL.DBDT.Rows
cbxStocksCategory.Items.Add(r("Category").ToString)
Next
cbxStocksCategory.Items.Add("")
End Sub
I'm trying to add a blank text to my combobox and I've successfully done it. Now my problem is when I select the blank text in my combobox, all of my data in my listview goes to blank data BUT when I select the items that is not blank text (as you can see in my for each loop, the Category is the column name and under that I have Drinks, Grilled etc.). My Listview shows the data which is base also on my query that you can see in if else condition.
What I want is when I select the blank text on my combobox. My Listview will show all the data like when it is first show (I mean without filtered)
If you, reading this cannot get specifically what I want please bear with me and I will try to edit it so you can get my thoughts.
Thank you.

Combobox dropdown choice freezing

My combo box (based off of a Query) drops down & shows options but I cannot click on an option & have it stay & fill in its related text boxes! I can highlight a customer in the dropdown but it won't let me "choose" one - all functioning completely stops. I can scroll through all 350 records at the bottom of the Form, but obviously not user-friendly.
OK, so to answer this fully I would need to know some other settings for the combo box. Can you tell me the Column count and Column widths? You say the combo box is based off a query (so this query is the Rowsource), and the Bound column is column 1; what is the first column shown in the query? Is it a numeric ID or is it the customer name?
To make the combo box 'unbound' you clear the Control source property. If you want to be able to edit the field that this combo was bound to then there are a couple of options, but that's probably a separate question. For now, to achieve the effect of displaying the right record when you change the combo selection, you need to add code to the AfterUpdate event:
Dim rst As Recordset
Set rst = Me.RecordsetClone
rst.FindFirst **search criteria**
If Not rst.NoMatch Then
Me.Bookmark = rst.Bookmark
End If
Set rst = Nothing
Depending on whether the first column of the query that your combo box is based off is numeric or text, you need to replace the search criteria with
"CustomerID = " & combo1
or
"CustomerName = '" & combo1 & "'"
(but with your own column name and combo box name)
Allen Browne has loads of useful tips on Access databases, check out http://allenbrowne.com/ser-03.html for a more comprehensive answer to this question.

Enter text from text box into combo box vb.net

I have two form, A and B.
On FORM A user will select a country code from combobox and it will then be saved to DB.
On FORM B a textbox shows the country code that was saved to the database earlier.
I want to change the country code in FORM B when edit is selected.
How to change:
1. the textbox will first be hidden
2. a combobox with all the country codes will be shown with selected value equals to the hidden textbox value.
I have tried putting the info into the combobox like the textboxes straight from the database when it is blank, e.g:
cbCountryCode.Text = CStr(dataTable.Rows(0).Item(2))
but this does not work. I also need to keep the country codes in the combobox as the user will need to change the country code if it's wrong. Is there a way of doing this? I have a work around where I don't let the user update the info if the combobox is blank, but I want the country code to be already there so that the user does not have to select a country code again if it is not wrong. Any help with this problem would be greatly appreciated.
EDIT:
datatable.Rows(0).Item(2)
holds a country code, for example, Ireland (+353), United Kingdom (+44) or U.S.A. (1).
This is the code I have for calling the information from the database:
sqlVisitorDetails = "SELECT * FROM visitorDetails WHERE idNumber=#idNumber"
sqlCon.Open()
sqlCmd = New SqlCommand(sqlVisitorDetails, sqlCon)
sqlCmd.Parameters.AddWithValue("#idNumber", txtIdNumber.Text)
dtVisitorDetails = loadDtVisitorDetails()
txtFirstName.Text = CStr(dtVisitorDetails.Rows(0).Item(1))
txtLastName.Text = CStr(dtVisitorDetails.Rows(0).Item(2))
txtContactNumber.Text = CStr(dtVisitorDetails.Rows(0).Item(3))
txtCountryCode.Text = CStr(dtVisitorDetails.Rows(0).Item(4))
txtAddress.Text = CStr(dtVisitorDetails.Rows(0).Item(5))
The country code (e.g. 'Ireland (+353)') is stored in dtVisitorDetails.Rows(0).Item(4) and this is put into the text box txtCountryCode.
When edit is clicked on the form, the text box txtCountryCode is hidden and the combobox cbCountryCode is visible (before edit is clicked txtCountryCode is shown and cbCountryCode is hidden). I then want the country code (in this case 'Ireland (+353)') to be shown in the cbCountryCode combo box. At the moment when the combobox is shown it is blank and the user has to choose a country code again, even if it's right. I hope this makes things clearer.
From the best i can understand from your question.
cbCountryCode.Text = CStr(dataTable.Rows(0).Item(2))
will not work if the DropDownStyle in the properties is set to DropDownList, change it to DropDown instead(if its not).
And to this:
I also need to keep the country codes in the combobox as the user will need to change the country code if it's wrong.
you have to bind the data to the ComboBox to make it work.
EDIT:
If possible, use ColumnName instead of Index for getting a data from datatable. Since you're selecting all record from your database, your may not know when then index would change(when a column is added or deleted from DB)
cbCountryCode.Text = CStr(dataTable.Rows(0).Item("CountryCodeColumn"))
How about that?
cbCountryCode.ClearSelection()
cbCountryCode.SelectedIndex = cbCountryCode.Items.IndexOf(cbCountryCode.Items.FindByText(datatable.Rows(0).Item(2).ToString))
Try this;
cbCountryCode.SelectedItem = CStr(dataTable.Rows(0).Item(2))
The .Text property is the currently selected text in the combo, not the currently selected item.
EDIT: (version 3!)
This definitely works! Create a new project, with a new WinForms project and Form1, etc. Add a button (btnTest) and a combobox (cboTest) and paste this code in;
Private Sub Form1_Load() Handles MyBase.Load
' Add some items
cboTest.Items.Add("U.S.A (+1)")
cboTest.Items.Add("Ireland (+353)")
cboTest.Items.Add("U.K. (+44)")
' Select the first item
cboTest.SelectedIndex = 0
End Sub
Private Sub btnTest_Click() Handles btnTest.Click
' Select the UK entry
cboTest.SelectedIndex = cboTest.FindString("U.K.")
End Sub
And I hate to have to say it, but it works like a charm! I've tested it with the combobox set to DropDown AND to DropDownList and the result is the same. Please check you have the right data going into and out of your routines from the dataTable/Row/Item you are using!!
Try simple:
comboBox.Items.Add("Item 1");

Remove row/item from RadGridView WPF

I have a code similar to next to load a data from a SQL query.....
Dim myDataset as new dataset = myMethod(params) 'This is a methos that fills a common dataset.
With Me.myRadDataGrid
.AutoGenerateColumns = True
.ItemsSource = myDataset.Tables(0).Rows
End With
So far so good....but when I tried to remove a line the object items do not nothing; the line do not show any exception, but nothing happend...
Me.myRadDataGrid.Items.RemoveAt(myIndex) 'Nohitng happend
Me.myRadDataGrid.Items.Remove(Me.myRadDataGrid.SelectItem) 'Nothig happend
Me.myRadDataGrid.Items.Refresh()
Me.myRadDataGrid.Rebind()
At the end the dataset collection into a RadGridView has the same elements....do not remove any row.
Thanks to all....
You need to remove the item from the ItemsSource, not from the Items collection.
So remove the item from myDataset.Tables(0).Rows, and you'll probably need to refresh the grid manually since I'm fairly sure a DataTable will not automatically raise a change notification like an ObservableCollection does when an item gets removed.

i want to bind two columns from Database into a combobox using vb.net

Can anybody help me.. or teach me how to use an alternative way just to bind it to a combo box. but it should show like a 2 column grid or a table.. not concatenate.
This is my existing code:
strSelectShi = "SELECT ShiftCode, Description FROM tbShift_shift"
daShi = New OleDbDataAdapter(strSelectShi, strConPay)
daShi.TableMappings.Add("Table", "tbShift_shift")
new dsShi = New DataSet
dsShi.Clear()
daShi.Fill(dsShi, "tbShift_shift")
With cbTue
.DataSource = dsShi.Tables("tbShift_shift")
.DisplayMember = "Shifcode"
.ValueMember = "Key"
End With
daShi.Dispose()
You can't display multiple columns in a combobox. What you could do is create a form with the table in a datagridview and open up that form when the user clicks on the combobox.
Code project file on Flat-MultiColumn Combobox

Resources