svgharmode
asked on
How to Set Combobox selectedIndex property to -1?
Hello Guys,
I am creating 1 windows appl. and in that i had created onw function as follows
Public Sub PopComboBox(ByVal objCombo As ComboBox, ByVal sTableName As String, ByVal sDisplayFieldName As String, ByVal sReferFieldName As String, Optional ByVal sAnyOtherField As String = "", Optional ByVal blnAddAll As Boolean = False)
Try
Dim SqlQuery As String = "Select " & Trim(sReferFieldName) & "," & Trim(sDisplayFieldName)
If sAnyOtherField <> "" Then
SqlQuery = SqlQuery & "," & Trim(sAnyOtherField)
End If
SqlQuery = SqlQuery & " FROM " & Trim(sTableName)
Dim sSortOrder As String = sDisplayFieldName & " ASC"
Dim SqlGenCmd As New SqlCommand(SqlQuery)
Dim sqlDAGenCode As New SqlDataAdapter(SqlGenCmd)
Dim dsForCombo As New DataSet()
Dim dvForCombo As DataView
If HasConnected = False Then
OpenConnection(sDBName)
End If
SqlGenCmd.Connection = sqlConn
sqlDAGenCode.Fill(dsForCom bo, sTableName)
dvForCombo = New DataView(dsForCombo.Tables (sTableNam e), "", sSortOrder, DataViewRowState.OriginalR ows)
objCombo.DataSource = dvForCombo
objCombo.DisplayMember = sDisplayFieldName
objCombo.ValueMember = sReferFieldName
If blnAddAll = True Then
Dim dRow As DataRow = dsForCombo.Tables(sTableNa me).NewRow
dRow = dsForCombo.Tables(0).NewRo w()
dRow(sDisplayFieldName) = "-----ALL-----"
dRow(sReferFieldName) = 0
dsForCombo.Tables(0).Rows. InsertAt(d Row, 0)
End If
sqlDAGenCode.Dispose()
dsForCombo.Dispose()
Catch SqlEx As SqlException
MsgBox(SqlEx.Message)
Catch Proex As Exception
MsgBox(Proex.Message)
End Try
End Sub
...... This function is called from the windows show events with the combobox and other parameters.
Now at the load event i written the code as
cmbMemberName.SelectedInde x=-1 .... but it is not working
please suggest me.
Thankx in advance.
Sanjeev
I am creating 1 windows appl. and in that i had created onw function as follows
Public Sub PopComboBox(ByVal objCombo As ComboBox, ByVal sTableName As String, ByVal sDisplayFieldName As String, ByVal sReferFieldName As String, Optional ByVal sAnyOtherField As String = "", Optional ByVal blnAddAll As Boolean = False)
Try
Dim SqlQuery As String = "Select " & Trim(sReferFieldName) & "," & Trim(sDisplayFieldName)
If sAnyOtherField <> "" Then
SqlQuery = SqlQuery & "," & Trim(sAnyOtherField)
End If
SqlQuery = SqlQuery & " FROM " & Trim(sTableName)
Dim sSortOrder As String = sDisplayFieldName & " ASC"
Dim SqlGenCmd As New SqlCommand(SqlQuery)
Dim sqlDAGenCode As New SqlDataAdapter(SqlGenCmd)
Dim dsForCombo As New DataSet()
Dim dvForCombo As DataView
If HasConnected = False Then
OpenConnection(sDBName)
End If
SqlGenCmd.Connection = sqlConn
sqlDAGenCode.Fill(dsForCom
dvForCombo = New DataView(dsForCombo.Tables
objCombo.DataSource = dvForCombo
objCombo.DisplayMember = sDisplayFieldName
objCombo.ValueMember = sReferFieldName
If blnAddAll = True Then
Dim dRow As DataRow = dsForCombo.Tables(sTableNa
dRow = dsForCombo.Tables(0).NewRo
dRow(sDisplayFieldName) = "-----ALL-----"
dRow(sReferFieldName) = 0
dsForCombo.Tables(0).Rows.
End If
sqlDAGenCode.Dispose()
dsForCombo.Dispose()
Catch SqlEx As SqlException
MsgBox(SqlEx.Message)
Catch Proex As Exception
MsgBox(Proex.Message)
End Try
End Sub
...... This function is called from the windows show events with the combobox and other parameters.
Now at the load event i written the code as
cmbMemberName.SelectedInde
please suggest me.
Thankx in advance.
Sanjeev
put you rcode in form activate event
regards
prakash
ASKER
Hello
Thankx for reply , but i dont want to select any vaue from combo box. rather i want to make combo box balnk when form is activatevated.
i add foolwing code to my form show Event()
PopComboBox (cmbMName,"MembeMaster","M Name","MCo de",,True)
It will fill the combobox and at time of load it will show the 0th record of combobox . i want there -1
cmbMName.SelectedIndex = -1 But it will not work.
it displays cmbMName's first record.
Please help for cmbMName.SelectedIndex = -1.....
Sanjeev
Thankx for reply , but i dont want to select any vaue from combo box. rather i want to make combo box balnk when form is activatevated.
i add foolwing code to my form show Event()
PopComboBox (cmbMName,"MembeMaster","M
It will fill the combobox and at time of load it will show the 0th record of combobox . i want there -1
cmbMName.SelectedIndex = -1 But it will not work.
it displays cmbMName's first record.
Please help for cmbMName.SelectedIndex = -1.....
Sanjeev
set the selected index property to two times
cmbMemberName.SelectedInde x=-1
cmbMemberName.SelectedInde x=-1
regrds
prakash
cmbMemberName.SelectedInde
cmbMemberName.SelectedInde
regrds
prakash
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hello Prakash,kaarthic
Thank u for replying,
But u r giving solution still not working. not able to set selectedindex = -1. Ok let me try again then if i got the solution i will tell u.
Sanjeev.
Thank u for replying,
But u r giving solution still not working. not able to set selectedindex = -1. Ok let me try again then if i got the solution i will tell u.
Sanjeev.
See that microsoft bug report about these.
http://support.microsoft.com/default.aspx?scid=kb;en-us;327244
Also see this discussion
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&c2coff=1&threadm=7QXyJWccDHA.2108%40cpmsftngxa06.phx.gbl&rnum=1&prev=/groups%3Fq%3Dbounded%2Bcombobox%2Bselectedindex%253D-1%26hl%3Den%26lr%3D%26ie%3DUTF-8%26c2coff%3D1%26selm%3D7QXyJWccDHA.2108%2540cpmsftngxa06.phx.gbl%26rnum%3D1
regards
prakash
http://support.microsoft.com/default.aspx?scid=kb;en-us;327244
Also see this discussion
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&c2coff=1&threadm=7QXyJWccDHA.2108%40cpmsftngxa06.phx.gbl&rnum=1&prev=/groups%3Fq%3Dbounded%2Bcombobox%2Bselectedindex%253D-1%26hl%3Den%26lr%3D%26ie%3DUTF-8%26c2coff%3D1%26selm%3D7QXyJWccDHA.2108%2540cpmsftngxa06.phx.gbl%26rnum%3D1
regards
prakash
cmbMemberName.Items.FindBy
if it throws error then try this, cmbMemberName.Items.FindBy