?
Solved

How to Set Combobox selectedIndex property to -1?

Posted on 2004-09-17
7
Medium Priority
?
224 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:svgharmode
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 4

Expert Comment

by:Kaarthick
ID: 12083662
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
0
 
LVL 10

Expert Comment

by:prakash_prk
ID: 12084257

put you rcode in form activate event

regards
prakash
0
 

Author Comment

by:svgharmode
ID: 12090726
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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:prakash_prk
ID: 12090767
set the selected index property to two times

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

regrds
prakash
0
 
LVL 4

Accepted Solution

by:
Kaarthick earned 375 total points
ID: 12090781
You try the following method. to select the default value in a combo box,

You add a default entry to combo, like '-----Chooose selection-----' and then assign the value for it as "", now make that item as the default selection when the page loads.

For example,

cmbMemberName.Items.Add("----Choose selection----")
cmbMemberName.Items(cmbMemberName.Items.Count - 1).Value = ""
cmbMemberName.Items(cmbMemberName.SelectedIndex).Selected = False
cmbMemberName.Items.FindByValue("").Selected = True

NOTE: Place the above code after you have loaded the actual values into the combobox. i.e. after binding the database values.
0
 

Author Comment

by:svgharmode
ID: 12119797
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.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question