P
Paul W
I have a combobox bound to a DataView, and I need to get a value from a
persisted DataTable using the ComboBox SelectedIndexChanged event (based on
the value of the item selected). I am successful in doing so, but I may be
doing it the long way around.
Currently, I am creating a DataRow Array and reading the value of a specific
column in the first row. The SelectedIndexChanged event code is listed
below, or you can download the sample project which is a working model using
the same code as below. Download project from
http://www.idhelp.com/ComboBoxTest.zip
If anyone has ideas or suggestions for doing this a better way, please let
me know.
Thanks, Paul
** Code **
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
ComboBox1.SelectedIndexChanged
If bLoadData Then
'data is being bound to object so ignore event
Return
Else
Dim rArray As DataRow() 'array of rows found in datatable
Dim r As DataRow, c As DataColumn
'find the data for the selected record if the selected value is
item was selectd
rArray = dsMain.Tables("tblTest").Select("myID = " &
ComboBox1.SelectedValue, "myID", DataViewRowState.CurrentRows)
If rArray.Length = 1 Then 'if not 1 row either there are no
rows or there is more than one row so we don't want any data
For Each r In rArray
For Each c In r.Table.Columns
TextBox1.Text = r("myValueToShow").ToString
Next
Next
Else
MsgBox("The information could not be resolved")
End If
End If
End If
End Sub
persisted DataTable using the ComboBox SelectedIndexChanged event (based on
the value of the item selected). I am successful in doing so, but I may be
doing it the long way around.
Currently, I am creating a DataRow Array and reading the value of a specific
column in the first row. The SelectedIndexChanged event code is listed
below, or you can download the sample project which is a working model using
the same code as below. Download project from
http://www.idhelp.com/ComboBoxTest.zip
If anyone has ideas or suggestions for doing this a better way, please let
me know.
Thanks, Paul
** Code **
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
ComboBox1.SelectedIndexChanged
If bLoadData Then
'data is being bound to object so ignore event
Return
Else
Dim rArray As DataRow() 'array of rows found in datatable
Dim r As DataRow, c As DataColumn
'find the data for the selected record if the selected value is
If ComboBox1.SelectedValue > 0 Then 'dont want data if first
item was selectd
rArray = dsMain.Tables("tblTest").Select("myID = " &
ComboBox1.SelectedValue, "myID", DataViewRowState.CurrentRows)
If rArray.Length = 1 Then 'if not 1 row either there are no
rows or there is more than one row so we don't want any data
For Each r In rArray
For Each c In r.Table.Columns
TextBox1.Text = r("myValueToShow").ToString
Next
Next
Else
MsgBox("The information could not be resolved")
End If
End If
End If
End Sub