K
Kay
Hi all,
Could you tell me the best way to add a blank item(as first item) in a data
binded combo box? Because I think I didn't do it right and it generate an
error if the second item(after the top blank item) is selected by using
Combo.SelectedValue :
"Specified argument was out of the range of valid values.
Parameter name: '-2147483648' is not a valid value for 'index'.
Could you guys please comment? Here's my code:
/////////////////////////////////////////
Sub LoadCombo(ByVal objCombo As ComboBox, ByVal pSQL As String)
Dim dsDataSet As New DataSet
Dim tblTable As DataTable
Dim myRow As DataRow
Dim myConn As SqlConnection
myConn = New SqlConnection
myConn.ConnectionString = gsConnStr
myConn.Open()
theAdapter = New SqlDataAdapter(pSQL, myConn)
dsDataSet.Clear()
theAdapter.Fill(dsDataSet, "Table")
tblTable = dsDataSet.Tables("Table")
''Put a blank row at the top '****
myRow = tblTable.NewRow
myRow("DisplayField") = ""
tblTable.Rows.InsertAt(myRow, 0)
objCombo.DataSource = Nothing
objCombo.Items.Clear()
objCombo.DataSource = tblTable
objCombo.DisplayMember = "DisplayField"
objCombo.ValueMember = "KeyField"
objCombo.SelectedValue = 0 '****
dsDataSet = Nothing
tblTable = Nothing
myConn.Close()
/////////////////////////////////////////
Thanks!
Kay
Could you tell me the best way to add a blank item(as first item) in a data
binded combo box? Because I think I didn't do it right and it generate an
error if the second item(after the top blank item) is selected by using
Combo.SelectedValue :
"Specified argument was out of the range of valid values.
Parameter name: '-2147483648' is not a valid value for 'index'.
Could you guys please comment? Here's my code:
/////////////////////////////////////////
Sub LoadCombo(ByVal objCombo As ComboBox, ByVal pSQL As String)
Dim dsDataSet As New DataSet
Dim tblTable As DataTable
Dim myRow As DataRow
Dim myConn As SqlConnection
myConn = New SqlConnection
myConn.ConnectionString = gsConnStr
myConn.Open()
theAdapter = New SqlDataAdapter(pSQL, myConn)
dsDataSet.Clear()
theAdapter.Fill(dsDataSet, "Table")
tblTable = dsDataSet.Tables("Table")
''Put a blank row at the top '****
myRow = tblTable.NewRow
myRow("DisplayField") = ""
tblTable.Rows.InsertAt(myRow, 0)
objCombo.DataSource = Nothing
objCombo.Items.Clear()
objCombo.DataSource = tblTable
objCombo.DisplayMember = "DisplayField"
objCombo.ValueMember = "KeyField"
objCombo.SelectedValue = 0 '****
dsDataSet = Nothing
tblTable = Nothing
myConn.Close()
/////////////////////////////////////////
Thanks!
Kay