How do I manually insert an item in a VB.net Combobox? - vb.net

I've created a Combobox and the list selections are being populated by a DataTable. I can populate it with no problems, but I need to add a default item for the list before the results from the DataTable appears.
The list should contain:
All Rooms and Facilities
Class Room
Laboratory
PE Facility
THE Facility
Drawing Room
Library
But I'm always getting:
I've been using this link as my resources:
https://msdn.microsoft.com/en-us/library/aa983551.aspx
And here's my code:
cboByRoomType.Items.Insert(0, "All Rooms and Facilities")
With cboByRoomType
.DataSource = tempDTRoomType
.DisplayMember = "Description"
.ValueMember = "Room Type ID"
.SelectedIndex = 0
End With
Also, I already tried to add the default item using the Items in the Properties Window, still no good.

Try replacing your first row with this (which probably has a more compact form):
tempDTRoomType.Rows.InsertAt(tempDTRoomType.NewRow(), 0)
tempDTRoomType.Rows(0).Item("Description") = "All Rooms and Facilities"
tempDTRoomType.Rows(0).Item("Room Type ID") = 0

Related

For a combobox, selectedIndex returns 0 while selectedtext returns a value

Once I select a value from a Combobox and save the form, ComboBox.SelectedText contains the value selected but ComboBox.SelectedIndex is returning 0 always for each item in the list. Below is just a sample code for reference.
If (combobox1.SelectedIndex = 0 Or combobox1.SelectedText = "")
MessageBox.Show("No value selected")
else
MessageBox.Show("Some value selected")
End If
Some code to illustrate usage
Private Sub ComboBox1_SelectedIndexChanged(sender As Object,
e As EventArgs) Handles ComboBox1.SelectedIndexChanged
'check for no item selected
If ComboBox1.SelectedIndex < 0 Then
Stop 'no item
Else
Dim idx As Integer = ComboBox1.SelectedIndex
Dim val As String = CStr(ComboBox1.SelectedItem) '<-- use SelectedItem
Stop
End If
End Sub
Small Answer
SelectedText is not the same as SelectedItem. Take a look at ComboBox.SelectedText:
Gets or sets the text that is selected in the editable portion of a ComboBox.
I think that you are confusing this with ComboBox.SelectedItem
SelectedIndex: -1 if nothing selected, otherwise the index of the selected item
SelectedValue: null if nothing selected, otherwise the selected item
SelectedText: the text that the operator marked in the editable part of the combo box.
** Room for improvement **
You use VB. My VB is a bit rusty, so I'll give my answer in C#. I guess you'll get the gist.
In Winforms, whenever you want to fill a that shows a sequence of items, like ComboBoxes, ListBoxes, DataGridViews, Charts, etc, there are usually two methods:
Fill the ComboBox one by one with the texts that you want to display
Use a DataSource: fill it with the Items that you want to be selectable, and tell the ComboBox which property of the selectable items you want to display.
Use the first method if you only want to display a constant array of strings. Fill ComboBox.Items. When an item is selected, use SelectedIndex to get the index of the selected string. Use ComboBox.Items[selectedIndex] to get the selected string.
If the string represents something more than just a string, for instance the text represents a Customer, or a Product. It is usually easier to use the DataSource method.
To do that, you use property ComboBox.DataSource to tell the ComboBox which Customersit should display. In ComboBox.ValueMember you tell the ComboBox which Customer poperty should be used to represent the Customer, for instance the name of the Customer.
Once the operator selected the name of the Customer, you get the complete Selected Customer using property ComboBox.SelectedItem:
List<Customer> availableCustomers = ...
ComboBox combo1 = new ComboBox(...);
combo1.ValueMember = nameof(Customer.Name); // the Customer property that you want to display
combo1.DataSource = availableCustomers;
After the operator selected an item, you can process the event, and fetch the selected customer immediately:
Customer selectedCustomer = (Customer)cmbo1.SelectedValue;
ProcessSelectedCustomer(selectedCustomer);
Of course you should only select a property that is unique. If you have two Customers named "Hans Brinker", operators wouldn't know which name represents which Customer.
Apart from the nice thing that you don't have to do a lookup from SelectedIndex to what this selected item represents (a Customer), you are independent of the order in which the Customers are displayed.
Another nice thing: if in future versions you want to change from ComboBox to ListBox, or maybe even to DataGridView, you won't have to change your model drastically: the control still shows a sequence of Customers, and once an operator selects something that represents this Customer (Name? Id?, DataGridView Row?), you get the complete Customer.
(1)ComboBox1.SelectedIndex starts from -1, when you are not selected, ComboBox1.SelectedIndex=-1
(2)When you click combobox1.items, SelectedText is always "".
This is because, at the time of these events(SelectedIndexChanged, SelectedValueChanged.. ), the previous SelectedText value has been cleared and the new value has not yet been set. You can use the SelectedItem property instead.
E.g:combobox1.SelectedItem
Please try the following code.
If ComboBox1.SelectedIndex = -1 Then
MessageBox.Show("No value selected")
Else
MessageBox.Show("Some value selected")
End If

combobox default value, vb.net

i have a combobox that populates from a database column but when the form loads, the combobox appears with the first item on the database. the value of this particular combobox determines the value of some other controls on the form so i want the combobox to load with an empty entry. thanks as i'll appreciate any help i can get.
here is my code:
'all variables declared...
connection.Open()
command = New MySqlCommand("SELECT PUMP FROM test.pump", connection)
dataadapter.SelectCommand = command
dataadapter.Fill(dataset)
With Me.PumpComboBox
.DisplayMember = "pump"
.DataSource = dataset
End With
Use
combobox1.SelectedIndex = -1
This sets the value of the combo box to a blank entry.
Maybe adding empty value and then selected that empty value using selectedindex after the combbobox populated will work
In your Form.Load include the following:
ComboBox1.SelectedIndex = 1
Here "ComboBox1" is your ComboBox and "1" equals index you want to be set by default.

How to set ComboBox/RepositoryItemComboBox datasource from a DataTable/TableAdapter?

I am wondering how can I set datasource to a ComboBox in case I want to load data from database everytime my grid loads up.
This is what I did for now (I hardcoded 3 items just to see is it working)
Dim riCombo As RepositoryItemComboBox = New RepositoryItemComboBox
riCombo.Items.AddRange(New String() {"London", "Berlin", "Paris"})
gridArticles.RepositoryItems.Add(riCombo)
colArticles.ColumnEdit = riCombo
I am getting data from database throught DataTableAdapter, there I am writing methods which are triggering sql queries.
After DIMITRY suggestions:
Dim riLookup As New RepositoryItemLookUpEdit()
riLookup.DataSource = DataTableDobTableAdapter.FillDob(Me.DsOrders.DataTableDob)
riLookup.ValueMember = "ID"
riLookup.DisplayMember = "TITLE"
riLookup.BestFitMode = DevExpress.XtraEditors.Controls.BestFitMode.BestFitResizePopup
GridView1.Columns("CODE").ColumnEdit = riLookup
GridView1.BestFitColumns()
But what I am getting here for column where valuesin dropdowns should be is next:
Thanks guys
Cheers
To fill the ComboBoxEdit editor's items, you can load data from your database to a list, then traverse all items in the list and add them to the Items collection. For example:
For Each row As DataRow In dataTable
Dim cellData As String = DirectCast(row(columnName), String)
riCombo.Items.Add(cellData)
Next
Also, you can use one of available Lookup Editors. These editors provide the DataSource property allowing you to load data from the data base to a list, and then assign this list to the DataSource property. Once this is done, simply set the KeyMember and ValueMember properties. This way, you don't need to fill items manually.

Having trouble getting data to show correctly in ComboBox

dbConn.Open()
Dim Query1 As String = cmbManufacturer.Text
Dim AccessDataAdapter = New System.Data.OleDb.OleDbDataAdapter
AccessDataAdapter.SelectCommand = New System.Data.OleDb.OleDbCommand("Select Models.[ModelName] From Models Where Manufacturer = '" & Query1 & "'", dbConn)
Dim AccessDataset = New DataSet
AccessDataAdapter.Fill(AccessDataset, "Query1")
cmbModel.DataSource = AccessDataset.Tables("Query1")
dbConn.Close()
So what this does is, it takes the selection from the manufacturer combo-box and determines the values that will display in the model combo-box
I've seen similar questions like this, but mostly in c# so I'm having trouble relating them. The problem is the output on the desired combo-box is
System.Data.DataRowView
when I need it to be the model names that it's pulling from the query. I changed this from a set of static entries in each combo box to make them dynamic, so in the future we wouldn't need to change the code to add or remove devices from the program.
When .DataSource is using for filling ComboBox with items, then DisplayMember used to retrieve a dipslayed text from the bounded object
From MSDN:
If the specified property does not exist on the object or the value of
DisplayMember is an empty string (""), the results of the object's
ToString method are displayed instead.
You are not used DisplayMember property, so method ToString() was called on the object, which is System.Data.DataRowView
You need to set a column name which you want use as displayed text
cmbModel.DisplayMember= "ModelName"
'If you will use a SelectedValue for furthers purposes
'Then you maybe want to set a ValueMember too
cmbModel.ValueMember= "ModelName"
cmbModel.DataSource = AccessDataset.Tables("Query1")
DisplayMember Property

combobox with first row blank

I have a a ComboBox bound to a DataSet. I would like to have combobox with very first row blank. How can I do this?
I've tried following
With .RoomComboBox
.DataSource = Me.aRoomsBindingSourse
.DisplayMember = "Room"
.ValueMember = "BedCode"
.DataBindings.Add("text", aRoomsBindingSourse, "Room", False,DataSourceUpdateMode.Never)
.SelectedIndex = -1
End With
Thank you in advance.
I don't think you can do this. Because .net framework doesn't allow to modify items if the DataSource set.
Hook into the post data bind event and add a row at the first location.