Home Forums WinForms controls Xceed Grid for WinForms IBindingList, CollectionBase, and comboboxes

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #16040 |

    I have an list of objects, implementing IBindingList, that I use as the datasource for a grid.  Each object in that list has, as a property, a collection of objects based on System.Collections.CollectionBase.  I’d like to present the collection in a combobox editor column with the values coming from the object in the list.

    For example, I create a list called CarList, which consists of Car objects.  Each Car object has a property ExtraOptionCollection which is a collection of objects of type ExtraOption.  Each Car object may have a different collection of ExtraOption objects in its ExtraOptionCollection property.  In the grid I would like each row (a Car object) to display the ExtraOptionCollection in a combobox, allowing the user to select a particular ExtraOption for that Car.

    I’ve searched the forums and the closest thread I could find is here.  But I’m still struggling to understand it and get a clear picture of how to proceed.  Any help would be appreciated.

    Thanks!
     

    Imported from legacy forums. Posted by slk23 (had 1268 views)

    Xceed Support
    Member
    Post count: 5658

    What you will need to do here is, assign an empty ComboBoxEditor has the CellEditorManager of the ExtraOptionCollection column. You subscribe to the ActivatingControl event (on the CellEditorManager) and you load the items according to the row you are in.

    Imported from legacy forums. Posted by CharlesB (had 685 views)

    User (Old forums)
    Member
    Post count: 23064

    Thanks Charles.  I’ve done as you suggested and it works well except for one thing: before the cell is clicked there aren’t any items in the combobox and the cell is empty.  While loading the items dynamically in the ActivatingControl event is okay, I’d like to be able to set a value programatically once the grid is loaded so the cell isn’t empty.

    I’m also a bit unclear on how to set up the column editor.  In my example the Car object has a ExtraOption property which is the currently selected ExtraOption for this Car.  The ExtraOptionList property has the list of ExtraOptions available to this particular Car.  I tried to tie the combobox to the ExtraOptionList but I never could get the column to display in the grid.  The only code I had success with is:

             // Initialize the editor to have a ComboBoxEditor as editor for the ExtraOption column.
            private void ConfigureExtraOptionColumnEditor(IList list)
            {
                Xceed.Grid.Editors.ComboBoxEditor comboBoxEditorManager = new Xceed.Grid.Editors.ComboBoxEditor();
                Xceed.Editors.WinComboBox comboBox = comboBoxEditorManager.TemplateControl;
                comboBox.Columns.Add(new Xceed.Editors.ColumnInfo(“ExtraOption”, typeof(ExtraOption)));
                OrdersGrid.Columns[“ExtraOption”].CellEditorManager = comboBoxEditorManager;
                OrdersGrid.Columns[“ExtraOption”].CellEditorManager.ActivatingControl +=
                      new Xceed.Grid.Editors.CellEditorEventHandler(ExtraOptionCell_ActivatingControl);
            }

    But now I find that when I assign a value to the ExtraOption property, the ActivatingControl event never fires and the cell doesn’t have the combobox control or items in it.  Can you see what I doing wrong?  I think it has something to do with the typeof I’m using:
    comboBox.Columns.Add(new Xceed.Editors.ColumnInfo(“ExtraOption”, typeof(ExtraOption)));

    But again when I used typeof(ExtraOptionList) the column didn’t display at all.

    Thanks in advance.

    Imported from legacy forums. Posted by slk23 (had 430 views)

    Xceed Support
    Member
    Post count: 5658

    The ActivatingControl event will only be triggered when the cell will get in “Edit mode”. If you want to change the value of the cell (from the “ExtraOption” column), you can directly assign the Value property of the cell. Also, to assign the selected value to the cell (when the Editor is left), you need to assign the DisplayMember property the right column from the ComboBoxEditor (in this case, ExtraOption).

    Here is a simple example on how to have a column of an Enum type with a ComboBox showing the choices:

    //Sample code:
    enum months : int
    {
      January,
      February,
      March,
      April,
      May,
      June,
      July,
      August,
      September,
      October,
      November,
      December
    };

    private void Form1_Load( object sender, EventArgs e )
    {
      Column col1 = new Column( “Items Column” );
      Column col2 = new Column( “Enum Column”, typeof( months ) );
      gridControl1.Columns.Add( col1 );
      gridControl1.Columns.Add( col2 );

      for( int i = 0; i < 20; i++ )
      {
        Xceed.Grid.DataRow row = gridControl1.DataRows.AddNew();
        row.Cells[ 0 ].Value = “Item ” + i.ToString();
        row.Cells[ 1 ].Value = ( months )( i % 12 );

        row.EndEdit();
      }

      ComboBoxEditor comboBoxEditor1 = new ComboBoxEditor();
      Array value = System.Enum.GetValues( typeof( months ) );
      for( int i = 0; i < value.Length; i++ )
      {
        comboBoxEditor1.Items.Add( value.GetValue( i ) );
      }
      comboBoxEditor1.ValueMember = “Column1”;

      gridControl1.Columns[ 1 ].CellEditorManager = comboBoxEditor1;
    }

    Imported from legacy forums. Posted by CharlesB (had 434 views)

    User (Old forums)
    Member
    Post count: 23064

    Thanks again Charles.  But there still seems to be something missing.  I need to populate each combobox in a column independently.  Each combobox may have different items compared the combobox in other rows.  The list of items comes from the DataRow SourceObject.  I can add the combobox items in the ActivatingControl event since I can access the DataRow SourceObject.  But I’d really like to add the combobox items either during the databinding of the grid, or immediately afterward by stepping through the rows.  I haven’t been able to do either one of those — any tips?

    Imported from legacy forums. Posted by slk23 (had 606 views)

    Xceed Support
    Member
    Post count: 5658

    The problem here is that the ComboBoxEditor does not exist until the user puts the cell in Edit mode. Before the cell gets into Edit mode, you can change the value of the cell directly.

    Imported from legacy forums. Posted by CharlesB (had 1258 views)

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.