Thread: ToolStripComboBox data binding issues

  1. #1
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607

    ToolStripComboBox data binding issues

    I am trying to bind a tool strip combo box to a data source but it is not working as expected.

    Code:
    class MapCellType : ModelBase
        {
            private int m_value;
            private Color m_color;
            private string m_name;
    
            public MapCellType()
            {
            }
    
            public int CellValue
            {
                get { return m_value; }
                set
                {
                    m_value = value;
                    OnPropertyChanged("CellValue");
                }
            }
    
            public Color Color
            {
                get { return m_color; }
                set
                {
                    m_color = value;
                    OnPropertyChanged("Color");
                }
            }
    
            public string CellName
            {
                get { return m_name; }
                set
                {
                    m_name = value;
                    OnPropertyChanged("CellName");
                }
            }
                    
            public void OnColorClicked(object sender,EventArgs e)
            {
                ColorDialog dlg = new ColorDialog();
                
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    Color = dlg.Color;
                }
            }
    
        }
    Data source:
    Code:
    private BindingList<MapCellType> m_mapCellTypes = new BindingList<MapCellType>();
    Binding code:
    Code:
    BindingSource source = new BindingSource();
    source.Add(m_mapCellTypes);
                
    tscboObjects.ComboBox.DataSource = source;
    tscboObjects.ComboBox.DisplayMember = "CellName";
    tscboObjects.ComboBox.ValueMember = "CellValue";
    tscboObjects.ComboBox.DataBindings.Add("SelectedItem", this, "MapValue",true,DataSourceUpdateMode.OnPropertyChanged);
    
    tscboObjects.ComboBox.BindingContext = this.BindingContext;
    Property in main form:
    Code:
    public int MapValue
            {
                get { return m_mapValue; }
                set
                {
                    m_mapValue = value;
                    usrMapEditor1.MapValue = m_mapValue;
                    usrMapEditor1.CellColor = m_mapCellTypes[tscboObjects.SelectedIndex].Color;
                }
            }
    ModelBase is a simple class that implements INotifyPropertyChanged and provides a method to fire this event.

    The combo box does not show the correct display values. It show what is specified in the value member which is not correct. Second the combo box is not being bound to the MapValue property. When the value changes in the combo box no breakpoints are hit within the setter of the MapValue property.

    Anyone else encountered this with tool strip combo boxes?

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Not sure this will provide you with the best solution but try adding the following:
    Code:
    tscboObjects.ComboBox.DataBindings.Add("Text", source, "CellName");
    Found this here: C# .net Combobox displays valuemember not displaymember when bound to dataset - Stack Overflow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure issues
    By junglist in forum C Programming
    Replies: 5
    Last Post: 02-16-2011, 04:17 PM
  2. binding to 0.0.0.0
    By Scramble in forum Linux Programming
    Replies: 1
    Last Post: 02-01-2011, 06:02 PM
  3. Data Alignment Issues...
    By aaba1 in forum C Programming
    Replies: 1
    Last Post: 10-03-2010, 11:03 AM
  4. Binding
    By zee in forum C Programming
    Replies: 6
    Last Post: 08-03-2004, 03:38 PM
  5. Static Binding & Dynamic Binding :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 12-31-2001, 08:51 PM