הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Tuesday, May 30, 2017 2:23 PM
Hi guys am having problems populating a combo box whose value depends on the selectedvalue of another combobox
The selected value.tostring method returns system.data.datarowview
The Image:
All replies (10)
Saturday, June 3, 2017 4:41 AM ✅Answered | 1 vote
Hello,
Here there are two ComboBoxes, on the left are choices, on the right items which match up to selections.
Form load would mimic loading data from a database.
Public Class Form1
Private dtProductList As New DataTable
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim dtCategory As New DataTable
dtCategory.Columns.Add(New DataColumn With
{
.ColumnName = "Identifier",
.DataType = GetType(Integer),
.AutoIncrement = True
})
dtCategory.Columns.Add(New DataColumn With
{
.ColumnName = "Text",
.DataType = GetType(String)
})
dtCategory.Rows.Add(New Object() {Nothing, "Select"})
dtCategory.Rows.Add(New Object() {Nothing, "Seafood"})
dtCategory.Rows.Add(New Object() {Nothing, "Produce"})
dtCategory.Rows.Add(New Object() {Nothing, "Grains/Cereals"})
selectionComboBox.DisplayMember = "Text"
selectionComboBox.ValueMember = "Identifier"
selectionComboBox.DataSource = dtCategory
dtProductList.Columns.Add(New DataColumn With
{
.ColumnName = "Category",
.DataType = GetType(Integer)
})
dtProductList.Columns.Add(New DataColumn With
{
.ColumnName = "Text",
.DataType = GetType(String)
})
dtProductList.Rows.Add(New Object() {2, "Manjimup Dried Apples"})
dtProductList.Rows.Add(New Object() {2, "Uncle Bob's Organic Dried Pears"})
dtProductList.Rows.Add(New Object() {2, "Rössle Sauerkraut"})
dtProductList.Rows.Add(New Object() {1, "Spegesild"})
dtProductList.Rows.Add(New Object() {1, "Escargots de Bourgogne"})
dtProductList.Rows.Add(New Object() {3, "Filo Mix"})
dtProductList.Rows.Add(New Object() {3, "Gnocchi di nonna Alice"})
dtProductList.Rows.Add(New Object() {3, "Gustaf's Knäckebröd"})
dtProductList.Rows.Add(New Object() {3, "Tunnbröd"})
dtProductList.Rows.Add(New Object() {0, ""}) ' when Select is selected
End Sub
Private Sub ComboBox1_SelectionChangeCommitted(sender As Object, e As EventArgs) _
Handles selectionComboBox.SelectionChangeCommitted
dtProductList.DefaultView.RowFilter = $"Category = {CInt(selectionComboBox.SelectedValue)}"
itemsComboBox.DataSource = dtProductList.DefaultView
itemsComboBox.DisplayMember = "Text"
itemsComboBox.Enabled = itemsComboBox.Items.Count > 0
End Sub
End Class
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator
Tuesday, May 30, 2017 2:45 PM
Hi
Try using .SelectedItem.ToString instead.
Regards Les, Livingston, Scotland
Tuesday, May 30, 2017 2:53 PM
@leshay Thanks for that idea, but it still returns the same value as system.data.datarowview.
Please any other idea will be appreciated.
Here is the image:
Please any further information you want from me i will still provide it, in case you don't understand.
Lots of Thanks!!!
Tuesday, May 30, 2017 3:02 PM | 1 vote
Here is a quick example of how to get back data on SelectedIndexChanged
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim dt As New DataTable
dt.Columns.Add(New DataColumn With {.ColumnName = "id", .DataType = GetType(Integer), .AutoIncrement = True})
dt.Columns.Add(New DataColumn With {.ColumnName = "Name", .DataType = GetType(String)})
dt.Rows.Add(New Object() {Nothing, "Karen"})
dt.Rows.Add(New Object() {Nothing, "Mary"})
dt.Rows.Add(New Object() {Nothing, "Jim"})
ComboBox1.DataSource = dt
ComboBox1.DisplayMember = "Name"
AddHandler ComboBox1.SelectedIndexChanged, AddressOf ComboBox1_SelectedIndexChanged
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs)
Dim item = CType(ComboBox1.SelectedItem, DataRowView).Row
MessageBox.Show(String.Join(",", item.ItemArray))
End Sub
End Class
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator
Tuesday, May 30, 2017 3:09 PM
@leshay Thanks for that idea, but it still returns the same value as system.data.datarowview.
Please any other idea will be appreciated.
Here is the image:
Please any further information you want from me i will still provide it, in case you don't understand.
Lots of Thanks!!!
Hi
This would have been my reply. However, it seems others have noticed that your CB data is not just a list of strings, so my offering is of no use to you.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBox1.Items.AddRange({"One", "Two", "Three"})
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim cb As ComboBox = DirectCast(sender, ComboBox)
' example: choose item "Three" in CB
Dim str As String = cb.SelectedItem.ToString
' str = "Three"
Dim lngth As Integer = cb.SelectedItem.ToString.Length
' lngth = 5
Dim b As Boolean = cb.SelectedItem.ToString.Length > 0
' b = True
End Sub
Regards Les, Livingston, Scotland
Thursday, June 1, 2017 9:29 AM | 1 vote
Hi Pat,
The problem is that the selected value must be returning an object of type System.Data.DataRowView.
This should be because of the way you initialized the comboBox.
For example, I initialized the combobox like this.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim dt As New DataTable()
dt.Columns.Add("Column1", GetType(Int32))
dt.Columns.Add("Column2", GetType(String))
dt.Rows.Add(1, "A")
dt.Rows.Add(2, "B")
dt.Rows.Add(3, "C")
dt.Rows.Add(4, "D")
ComboBox1.DataSource = dt
ComboBox1.DisplayMember = "Column2"
End Sub
In order to get the value wanted, you should ask the System.Data.DataRowView object to print the "Column1" column which is the one you're wanting (you can otherwise get the column "Column2").
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
MessageBox.Show(ComboBox1.SelectedValue("Column1"))
MessageBox.Show(ComboBox1.SelectedValue("Column2").ToString())
End Sub
Best Regards,
Cherry
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Thursday, June 1, 2017 9:59 AM | 1 vote
Hi Cherry,
I would suggest writing the code to work with Option Strict On as with Option Strict On the following will not compile.
MessageBox.Show(ComboBox1.SelectedValue("Column1"))
MessageBox.Show(ComboBox1.SelectedValue("Column2").ToString())
I would suggest the following.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim dt As New DataTable()
dt.Columns.Add("Column1", GetType(Int32))
dt.Columns.Add("Column2", GetType(String))
dt.Rows.Add(1, "A")
dt.Rows.Add(2, "B")
dt.Rows.Add(3, "C")
dt.Rows.Add(4, "D")
ComboBox1.DataSource = dt
ComboBox1.DisplayMember = "Column2"
AddHandler ComboBox1.SelectedIndexChanged, AddressOf ComboBox1_SelectedIndexChanged
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs)
Dim row As DataRow = CType(ComboBox1.SelectedValue, DataRowView).Row
Dim Col1 As Integer = row.Field(Of Integer)("Column1")
Dim Col2 As String = row.Field(Of String)("Column2")
MessageBox.Show($"Col1: {Col1} Col2: {Col2}")
End Sub
End Class
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator
Thursday, June 1, 2017 10:05 AM | 1 vote
Pat,
Some think that ToString means set it to characters. That is not true.
The not overridden ToString property returns the name (type) of object which has this member. In this case it is DataRowView
ToString to get characters is used by value types. It is called overridding of the member. (methods and properties)
https://msdn.microsoft.com/en-us/library/818x9db9(v=vs.90).aspx
Normally is this what is returned with ToString if it is not oversridden
https://msdn.microsoft.com/en-us/library/system.object.tostring(v=vs.110).aspx
Therefore you should not use ToString but a convert method (or a VB function which does the same likewise CStr)
Success
Cor
Saturday, June 3, 2017 1:45 AM
You are such a wonderful instructor, thanks for that idea in fact i saw the correct selected item of the combo box each time i step through my code in the item.ItemArray. But the problem is how to return it to the combo box and as well use that populated information and fill the next combo box whose value depends on the former combo box. With your idea i think something can be done, let me try. Lots of thanks Instructor
Saturday, June 3, 2017 4:42 PM
You are good Instructor. It's working and thanks to all who contributed.
Yo guys are great!!! Thumbs Up