#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    13
    Rep Power
    0

    Combobox changing its index by itself


    hi,

    i have ComboBox in which I have names of employees.

    I created ComboBox_SelectedIndexChanged so when I pick something from ComboBox, I take employee and do something [write somethin into TextBox]

    Works fine, but I have problem when I have employees with same name: when I click to TextBox or some button, SelectedIndex is changed to index of employee with same name, but smaller index [i mean the one that is "upper" in ComboBox items]!

    Any idea why? Can I make this behaviour stop?

    For Combobox I have this class, because I need to show and receive different stuff:

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Diploma
    {
        //own class for combobox to show and use different item
        class ComboBoxItem
        {
            private string name; // sth to show, eg employee name and surname
            private string value;  //eg: employee_id- make sure unique 
    
            public string Value
            {
                get { return this.value; }
                set { this.value = value; }
            }
            public string Name
            {
                get { return name; }
                set { name = value; }
            }
    
            public ComboBoxItem(string nName, string nValue)
            {
                this.name = nName;
                this.value = nValue;
            }
    
            public override string ToString()
            {
                return this.name;
            }
        }
    }
    I have visual studio 2010, project is in C#
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2007
    Location
    Charlotte
    Posts
    412
    Rep Power
    144
    A combobox has two properties on the page... the *value* and the *text*. The text property is what is shown on the page to the user. The value property is how you interact with the combo box. I'm thinking the problem you're having is that maybe the value properties have to be unique?

    While you could have two "John Smith" text properties, but not two "John Smith" value properties. You would get around this possibly by having an employee ID as the value property.

    As a test... while creating the combobox, use a counter and increment and make that the value part, when you select, see if that gives the appropriate one.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    13
    Rep Power
    0
    thanks for ideas!

    anyway I think I found out whats the reason of this behaviour:

    i have AutoCompleteMode property set to SuggestAttach.

    when set to None, behaviour is normal, nothing is changing. Any other work as I said earlier. Maybe someone had similar problem before?

    I add stuff to ComboBox like this:
    Code:
    foreach (DataRow r in dt2.Rows)
                    {
                        delEmpChooseComboBox.Items.Add(new ComboBoxItem(r["name"] + " " + r["surname"], r["employee_id"].ToString()));
                    }
    thing is that I use this to pick what employee I wanna delete - but, when I click on delete button, the employee is changed to employee with same name, but higher at the ComboBox, so I am not able to delete the one I wanna!

    Thanks for ideas!

IMN logo majestic logo threadwatch logo seochat tools logo