Answered System.Data.DataRow.View

Socarsky

Well-known member
Joined
Dec 27, 2012
Messages
173
Location
Jakarta/Indonesia
Programming Experience
Beginner
There is a binding that retrieves data as name into a combobox control. But I see that messagebox every time when the program runs and after an item selected on the dropbox. I want to learn the reason..

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        Dim selectedIndex As Integer
        selectedIndex = ComboBox1.SelectedIndex
        Dim selectedItem As Object
        selectedItem = ComboBox1.SelectedItem
        MessageBox.Show("Selected Item Text: " & selectedItem.ToString() & vbCrLf & "Index: " & selectedIndex.ToString())
    End Sub


4prl.jpg
 
Last edited:
Firstly, note that it's 'System.Data.DataRowView', not 'System.Data.DataRow.View'.

When you bind a DataTable to a control, the data actually comes from the DefaultView of the DataTable, which is a DataView. Each item in that DataView is a DataRowView, therefore each item in your ComboBox is a DataRowView. If each item is a DataRowView then the selected item is a DataRowView, which is why you see that type name when you call ToString on the SelectedItem.

Presumably, what you actually want is the text displayed in the ComboBox for that item. The ComboBox has a GetItemText method that will get the text for any item, so you could do this:
ComboBox1.GetItemText(ComboBox1.SelectedItem)
You don't even have to do that though, because if you want the text of the selected item then you can simply use the Text property.
 
Firstly, note that it's 'System.Data.DataRowView', not 'System.Data.DataRow.View'.
Presumably, what you actually want is the text displayed in the ComboBox for that item. The ComboBox has a GetItemText method that will get the text for any item, so you could do this:
ComboBox1.GetItemText(ComboBox1.SelectedItem)
You don't even have to do that though, because if you want the text of the selected item then you can simply use the Text property.
First of all thanks for your helping.

That action which you mentioned that brings a problem that ComboBox1_SelectedIndexChanged event calls even the program load automatically and the event runs once. It's not acceptable for me to face with it before the form's interface show up.. this comes from DataRowView as you mentioned and I had to solve that issue with this way..
Dim num As Short = -1
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        num = CShort(num + 1)
        Dim cmbStr As String
        cmbStr = ComboBox1.GetItemText(ComboBox1.SelectedItem)
        If num <> 0 Then
            MessageBox.Show("Iste Text: " & cmbStr)
        End If
    End Sub
 
If you want to do something only when the user makes a selection in the ComboBox rather than every time the SelectedIndex changes regardless of cause then handle the SelectionChangeCommitted event instead of SelectedIndexChanged.
 
Back
Top