Link to home
Start Free TrialLog in
Avatar of svgharmode
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(dsForCombo, sTableName)

            dvForCombo = New DataView(dsForCombo.Tables(sTableName), "", sSortOrder, DataViewRowState.OriginalRows)
            objCombo.DataSource = dvForCombo
            objCombo.DisplayMember = sDisplayFieldName
            objCombo.ValueMember = sReferFieldName
            If blnAddAll = True Then
                Dim dRow As DataRow = dsForCombo.Tables(sTableName).NewRow
                dRow = dsForCombo.Tables(0).NewRow()
                dRow(sDisplayFieldName) = "-----ALL-----"
                dRow(sReferFieldName) = 0
                dsForCombo.Tables(0).Rows.InsertAt(dRow, 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.SelectedIndex=-1 .... but it is not working


please suggest me.

Thankx in advance.

Sanjeev
Avatar of Kaarthick
Kaarthick

tru this code instead of yours for the selection

cmbMemberName.Items.FindByValue(-1).Selected = True,

if it throws error then try this, cmbMemberName.Items.FindByValue("-1").Selected = True

put you rcode in form activate event

regards
prakash
Avatar of svgharmode

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","MName","MCode",,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
set the selected index property to two times

cmbMemberName.SelectedIndex=-1
cmbMemberName.SelectedIndex=-1

regrds
prakash
ASKER CERTIFIED SOLUTION
Avatar of Kaarthick
Kaarthick

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.