Bound ComboBox SelectedIndex Defaults to 0 
Author Message
 Bound ComboBox SelectedIndex Defaults to 0

Hi all,

I've run into an issue with a combobox bound to a datatable. No matter what
I try, it will always select the first element (0). Directly after binding I
set the SelectedIndex to -1 and this still doesn't work as it will reset
itself.

In this case, the list boxes are used to facilitate user queries. While I
could ensure there was a null value to indicate no option selected as the
first item (by either adding it to the database table or manually loading
the data into the combo and ensuring the first item is an empty string) this
seems ridiculous.

If I set the SelectedIndex to a -1 then I should have nothing selected. If
any one has found a work around other than adding an empty item into element
zero, I'd love to hear it.

Thanks,

Erik



Sun, 14 Nov 2004 23:53:08 GMT  
 Bound ComboBox SelectedIndex Defaults to 0

Quote:
> Hi all,

> I've run into an issue with a combobox bound to a datatable. No matter
what
> I try, it will always select the first element (0). Directly after binding
I
> set the SelectedIndex to -1 and this still doesn't work as it will reset
> itself.

> In this case, the list boxes are used to facilitate user queries. While I
> could ensure there was a null value to indicate no option selected as the
> first item (by either adding it to the database table or manually loading
> the data into the combo and ensuring the first item is an empty string)
this
> seems ridiculous.

> If I set the SelectedIndex to a -1 then I should have nothing selected. If
> any one has found a work around other than adding an empty item into
element
> zero, I'd love to hear it.

> Thanks,

> Erik

Could you post your code so that we can have a look why it defaults to 0?


Mon, 15 Nov 2004 00:16:55 GMT  
 Bound ComboBox SelectedIndex Defaults to 0


Quote:

> Could you post your code so that we can have a look why it defaults to 0?

All I'm doing is this, where dtStatus is of type DataTable.:
        cboStatus.DataSource = dtStatus

        cboStatus.DisplayMember = "IC_TEXT_STATUS"

        cboStatus.ValueMember = "IC_CODE_STATUS"

The only time I reference the SelectedIndex is to verify it isn't -1:

        If cboStatus.SelectedIndex <> -1 Then

                strSQL += "AND i.ic_code_status = " &
cboStatus.SelectedValue.ToString & " "

        End If

Even if I add cboStatus.SelectedIndex = -1 after binding, it still doesn't
take hold longer than the life of the sub.



Mon, 15 Nov 2004 00:44:34 GMT  
 Bound ComboBox SelectedIndex Defaults to 0

Quote:



> > Could you post your code so that we can have a look why it defaults to
0?

> All I'm doing is this, where dtStatus is of type DataTable.:
>         cboStatus.DataSource = dtStatus

>         cboStatus.DisplayMember = "IC_TEXT_STATUS"

>         cboStatus.ValueMember = "IC_CODE_STATUS"

> The only time I reference the SelectedIndex is to verify it isn't -1:

>         If cboStatus.SelectedIndex <> -1 Then

>                 strSQL += "AND i.ic_code_status = " &
> cboStatus.SelectedValue.ToString & " "

>         End If

> Even if I add cboStatus.SelectedIndex = -1 after binding, it still doesn't
> take hold longer than the life of the sub.

And you don't have any other databindings with cboStatus ? For instance,
cboStatus.DataBindings.Add("Text", ....)


Mon, 15 Nov 2004 00:58:04 GMT  
 Bound ComboBox SelectedIndex Defaults to 0


Quote:

> And you don't have any other databindings with cboStatus ? For instance,
> cboStatus.DataBindings.Add("Text", ....)

Nope, that's all there is. The annoying part is that the app that I'm
coverting would generate the query as soon as the user selected a new option
from the combo (a PowerBuilder app BTW). The SelectedIndexChange event for
the combo fires multiply times during the bind process. I actually have 3
combos: status, holder, business group, all of which have their data loaded
as such:

combo.DataSource = DataTable
combo.DisplayMember = "FieldName"
combo.ValueMember = "FieldName"

I throw up a message box in the SelectedIndexChange (SIC) event to give the
# times its displayed and the current index item. I bind these during the
form_Load event. This is what occurs

cboStatus
SIC event fires 3 times when I set the datasource
SIC event fires 2 times when I set the display member

cboHolder
SIC event fires 2 times when DataSource set
SIC event fires 2 times when DisplayMember set.

cboBusinessGroup
SIC event fires 2 times when DataSource set
SIC event fires 2 times when DisplayMember set

Immediatly After End Sub of Form_Load event
SIC event fires 6 more times (assumablly twice for each control as I have
one sub handling all of the SIC events).

While stepping thru the code via F8, it is interesting to note that unless I
place a break point in the SIC event, I never step into that code, the
messagebox I coded just displays.

I've gotten around the search routine being called in the
SelectedIndexChange event by having a form global variable that is set to
true when one of the combo's is clicked. Otherwise, the same query is
executed multiple times when the form loads.



Mon, 15 Nov 2004 01:51:13 GMT  
 Bound ComboBox SelectedIndex Defaults to 0
If the binding occurs in the Page_Load event, please try the following code:

If (Not IsPostBack) Then

    'Binding code here
End If

Gary

This posting is provided "AS IS", with no warranties, and confers no
rights. Enjoyed ASP.NET? http://www.asp.net



Mon, 15 Nov 2004 17:02:09 GMT  
 Bound ComboBox SelectedIndex Defaults to 0

Quote:
> If the binding occurs in the Page_Load event, please try the following
code:

> If (Not IsPostBack) Then

>     'Binding code here
> End If

> Gary

> This posting is provided "AS IS", with no warranties, and confers no
> rights. Enjoyed ASP.NET? http://www.asp.net

Sorry, I should have mentioned, this is a windows application, not ASP. As
far as I can tell, the IsPostBack is for web apps.

It'd be nice of MS broke this newsgroup into two, one specifically for
desktop apps, the other for ASP.

Thanks for the tip though.



Mon, 15 Nov 2004 21:09:46 GMT  
 Bound ComboBox SelectedIndex Defaults to 0
I have tried it with the followig code (I didn't use the data from
database. Instead, I created a structure), and it didn't behave as you
mentioned:

Public Structure State
    Private m_shortName, m_longName As String

    Public Sub New(ByVal longName As String, ByVal shortName As String)
        Me.m_shortName = shortName
        Me.m_longName = longName
    End Sub

    Public ReadOnly Property ShortName() As String
        Get
            Return m_shortName
        End Get
    End Property

    Public ReadOnly Property LongName() As String
        Get
            Return m_longName
        End Get
    End Property
End Structure

Public Class Form4
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox
    <System.Diagnostics.De{*filter*}StepThrough()> Private Sub
InitializeComponent()
        Me.ComboBox1 = New System.Windows.Forms.ComboBox()
        Me.SuspendLayout()
        '
        'ComboBox1
        '
        Me.ComboBox1.Location = New System.Drawing.Point(40, 72)
        Me.ComboBox1.Name = "ComboBox1"
        Me.ComboBox1.Size = New System.Drawing.Size(208, 21)
        Me.ComboBox1.TabIndex = 0
        Me.ComboBox1.Text = "ComboBox1"
        '
        'Form4
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 267)
        Me.Controls.AddRange(New System.Windows.Forms.Control()
{Me.ComboBox1})
        Me.Name = "Form4"
        Me.Text = "Form4"
        Me.ResumeLayout(False)

    End Sub

#End Region
    Private States() As State = {New State("Null", "Null"), New
State("Alabama", "AL"), New State("Wisconsin", "WI"), New State("Wyoming",
"WY")}

    Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
        ComboBox1.DataSource = States
        ComboBox1.DisplayMember = "LongName"
        ComboBox1.ValueMember = "ShortName"
    End Sub
End Class

Did I misunderstood you?

Gary

This posting is provided "AS IS", with no warranties, and confers no
rights. Enjoyed ASP.NET? http://www.*-*-*.com/



Tue, 16 Nov 2004 12:06:38 GMT  
 Bound ComboBox SelectedIndex Defaults to 0
Hello.

This post was some time ago, and I couldn't find where the
issue was resolved, so I will reissue the question, as I'm
having precisely the same problem as the original person
posting.

If you remove the array entry
New State("Null", "Null")
the combo box initially displays the first valid entry
(ie. Alabama).
If you leave the above entry, the combo box initially
displays "Null", and this appears as a choice in the drop-
down list also.

The goal here is to have the combo box edit field empty
initially.  Setting the SelectedItem property to Nothing
or "" does not work, nor does setting the SelectedIndex
property to -1 and the Text property to "".

Does anyone know how to work around this?

Thanks,
Jim.

Quote:
>-----Original Message-----
>I have tried it with the followig code (I didn't use the
data from
>database. Instead, I created a structure), and it didn't
behave as you
>mentioned:

>Public Structure State
>    Private m_shortName, m_longName As String

>    Public Sub New(ByVal longName As String, ByVal

shortName As String)
Quote:
>        Me.m_shortName = shortName
>        Me.m_longName = longName
>    End Sub

>    Public ReadOnly Property ShortName() As String
>        Get
>            Return m_shortName
>        End Get
>    End Property

>    Public ReadOnly Property LongName() As String
>        Get
>            Return m_longName
>        End Get
>    End Property
>End Structure

>Public Class Form4
>    Inherits System.Windows.Forms.Form

>#Region " Windows Form Designer generated code "

>    Public Sub New()
>        MyBase.New()

>        'This call is required by the Windows Form
Designer.
>        InitializeComponent()

>        'Add any initialization after the

InitializeComponent() call
Quote:

>    End Sub

>    'Form overrides dispose to clean up the component
list.
>    Protected Overloads Overrides Sub Dispose(ByVal

disposing As Boolean)
Quote:
>        If disposing Then
>            If Not (components Is Nothing) Then
>                components.Dispose()
>            End If
>        End If
>        MyBase.Dispose(disposing)
>    End Sub

>    'Required by the Windows Form Designer
>    Private components As System.ComponentModel.IContainer

>    'NOTE: The following procedure is required by the

Windows Form Designer
Quote:
>    'It can be modified using the Windows Form Designer.  
>    'Do not modify it using the code editor.
>    Friend WithEvents ComboBox1 As

System.Windows.Forms.ComboBox

- Show quoted text -

Quote:
>    <System.Diagnostics.De{*filter*}StepThrough()> Private
Sub
>InitializeComponent()
>        Me.ComboBox1 = New System.Windows.Forms.ComboBox()
>        Me.SuspendLayout()
>        '
>        'ComboBox1
>        '
>        Me.ComboBox1.Location = New System.Drawing.Point
(40, 72)
>        Me.ComboBox1.Name = "ComboBox1"
>        Me.ComboBox1.Size = New System.Drawing.Size(208,
21)
>        Me.ComboBox1.TabIndex = 0
>        Me.ComboBox1.Text = "ComboBox1"
>        '
>        'Form4
>        '
>        Me.AutoScaleBaseSize = New System.Drawing.Size(5,
13)
>        Me.ClientSize = New System.Drawing.Size(292, 267)
>        Me.Controls.AddRange(New

System.Windows.Forms.Control()
Quote:
>{Me.ComboBox1})
>        Me.Name = "Form4"
>        Me.Text = "Form4"
>        Me.ResumeLayout(False)

>    End Sub

>#End Region

*********************** GET RID OF FIRST ARRAY ENTRY TO
SEE PROBLEM ********************************************
Quote:
>    Private States() As State = {New State

("Null", "Null"), New

- Show quoted text -

Quote:
>State("Alabama", "AL"), New State("Wisconsin", "WI"), New
State("Wyoming",
>"WY")}

>    Private Sub Form4_Load(ByVal sender As System.Object,
ByVal e As
>System.EventArgs) Handles MyBase.Load
>        ComboBox1.DataSource = States
>        ComboBox1.DisplayMember = "LongName"
>        ComboBox1.ValueMember = "ShortName"
>    End Sub
>End Class

>Did I misunderstood you?

>Gary

>This posting is provided "AS IS", with no warranties, and
confers no
>rights. Enjoyed ASP.NET? http://www.*-*-*.com/

>.



Tue, 01 Feb 2005 04:51:54 GMT  
 Bound ComboBox SelectedIndex Defaults to 0
Does my code I posted before work on your side?

Gary

This posting is provided "AS IS", with no warranties, and confers no
rights. Enjoyed ASP.NET? http://www.asp.net



Tue, 01 Feb 2005 16:04:07 GMT  
 Bound ComboBox SelectedIndex Defaults to 0
Essentially, yes, it does. If I were to run your code, I would end up with a
combo box in which the edit field initially contained the string "Null".   I
realized after I made the post there was one variation in how I was going
about this compared to your example.  In your example, you loaded the combo
box in response to the form's load event, whereas I am loading mine in the
constructor for the form.  I altered my code so the
"combobox.selectedItem=nothing" lines were in the load event handler and the
combo boxes then behaved properly except for one (there are four in total).
I am currently trying to figure out why this one is different.  In this one,
the string name assigned to the ValueMember property differs from the field
name the SelectedValue is bound to, whereas in the others they are the same.
I'm going to see if that makes a difference...

Jim.

Quote:
> Does my code I posted before work on your side?

> Gary

> This posting is provided "AS IS", with no warranties, and confers no
> rights. Enjoyed ASP.NET? http://www.asp.net



Tue, 01 Feb 2005 23:17:14 GMT  
 Bound ComboBox SelectedIndex Defaults to 0
Jim,

Have you resolved this issue yet? I have the same problem. My drop
down list is populated with 50 state codes (e.g. 'CA'). This field on
the form can take a NULL. I can't believe I would have to create a
51st record in the state table to accommodate this 'feature'.

Once I issue the DataBind method, the SelectedIndex property will not
accept a -1!

Larry Marshall



Sat, 05 Feb 2005 08:55:44 GMT  
 Bound ComboBox SelectedIndex Defaults to 0
I've been fighting with the same problem myself., and have come up some
(IMHO) silly work arounds:

When I first bring up a form with a data bound combobox, and I want that
combobox to NOT default to the first value in the list, I issue a
selectedindex = -1 immediately after establishing binding.

ie:
I have an array filled the simple structure that follows:
--------------------
Private Class ComboData
    Private mText As String
    Private mID As Object
    Public ReadOnly Property ID() As Object
        Get
            Return mID
        End Get
    End Property
    Public ReadOnly Property Text() As String
        Get
            Text = mText
        End Get
    End Property
    Public Sub New(ByVal i As Object, ByVal t As String)
        mText = t
        mID = i
    End Sub
End Class
-------------

I then populate the array as follows:

------------
Private myArray() As ComboData = New ComboData() { _
    New ComboData(0, "Text"), _
    New ComboData(1, "General Numeric"), _
    New ComboData(2, "Date"), _
    New ComboData(3, "Yes/No"), _
    New ComboData(4, "Currency")}
------------

I then setup my ComboBox as follows:

---------
cbo.DataSource = myArray
cbo.DisplayMember = "Text"
cbo.ValueMember = "ID"
cbo.SelectedIndex = -1
---------

If I don't toss in the "cbo.SelectedIndex = -1" then the combobox ALWAYS
defaults to the first value in the list.

Even more frustrating is if I want to clear out the combobox after a user
has selected a value.  The ONLY way I've found how to do this is to is to
set the SelectedIndex property to -1 twice...

ie:
---------
cbo.SelectedIndex = -1
cbo.SelectedIndex = -1
---------

The first time I set it to -1, the cbo.SelectedIndex is actually set to 0.
Then when I set it again to -1, it actually does work and get set properly.

This seems like VERY odd behavior, and I'm wondering if I'm doing something
wrong, or if I have an old version of the system.win.forms DLL.  According
to my GAC, I have verion "1.0.3300.0" is that the right one?

Any help is appreciated.

Roy


Quote:
> Jim,

> Have you resolved this issue yet? I have the same problem. My drop
> down list is populated with 50 state codes (e.g. 'CA'). This field on
> the form can take a NULL. I can't believe I would have to create a
> 51st record in the state table to accommodate this 'feature'.

> Once I issue the DataBind method, the SelectedIndex property will not
> accept a -1!

> Larry Marshall



Wed, 23 Feb 2005 12:58:26 GMT  
 
 [ 13 post ] 

 Relevant Pages 

1. Bound ComboBox SelectedIndex Defaults to 0

2. SelectedIndex not corresponding with .text in ComboBox

3. ComboBox.SelectedIndex = Nothing problem...

4. Combobox click/selectedindex problems

5. combobox selectedindex

6. Synchronizing bound combobox and bound textboxes?

7. Referencing a combobox value and the concept of the bound column

8. Partialy bound ComboBox?

9. Multi-Column Data-Bound ComboBox

10. Binding Combobox to SQLServer

11. Multi column Bind ComboBox ??

12. Listbox/Combobox binding problem

 

 
Powered by phpBB® Forum Software