Infralution Support Forum Index Infralution Support
Support groups for Infralution products
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Editable combobox as cell editor

 
Post new topic   Reply to topic    Infralution Support Forum Index -> Virtual Tree Support
View previous topic :: View next topic  
Author Message
AB



Joined: 20 Nov 2007
Posts: 23
Location: Netherlands

PostPosted: Tue Nov 20, 2007 1:09 pm    Post subject: Editable combobox as cell editor Reply with quote

In my treeview, I'd like to use a combobox as a cell editor, in which a user can select from the dropdown list, but also edit a value (which isn't added to the dropdown list).
To do this, in the designer I added an editor as System.Windows.Forms.DataGridViewComboBoxEditingControl. In the List-property I added some text values. Then I create a new column and select my new editor as CellEditor.
However, when running the project (VB.NET), I can see the combobox, but it is not filled with items, so there is nothing to select. What am I doing wrong?
Is there another way of creating this type of combobox?
Back to top
View user's profile Send private message Visit poster's website
Infralution



Joined: 28 Feb 2005
Posts: 5027

PostPosted: Tue Nov 20, 2007 10:12 pm    Post subject: Reply with quote

The editor control (in this case the ComboBox) is just a template for the control(s) actually used by VirtualTree to do the editing - it is not the actual control used. The reflection mechanism used to copy the properties of the template control can cope with most common properties - but the list of items for combo box is beyond its capabilities.

For this reason we provide a separate mechanism to allow you to initialise more complex properties such as this. Add a handler to the CellEditor.InitializeControl event. This is fired prior to the control being displayed - and gives you a chance to initialize properties. The editor controls are cached and reused for performance so this also gives you a chance to reset properties to the defaults if necessary eg:

Code:
private void OnInitializeComboControl(object sender, CellEditorInitializeEventArgs e)
{
    ComboBox comboBox = (ComboBox)e.Control;
    if (e.NewControl)
    {
          // add the list items
          comboBox .Items.Add("Test");
    }
}


Am alternative is to use the DataSource property (instead of initializing List items). The DataSource property is copied by CellEditor from the templace control automatically. Thus you could create a list of strings and set this as the DataSource in the template control.

Note that you do not have to use a DataGridViewComboBoxEditingControl - you can use the standard ComboBox as the editor control. In fact I would probably recommend this.
_________________
Infralution Support
Back to top
View user's profile Send private message Visit poster's website
AB



Joined: 20 Nov 2007
Posts: 23
Location: Netherlands

PostPosted: Tue Jun 10, 2008 2:32 pm    Post subject: Reply with quote

Ok, it works but I don't understand the difference between setting the Datasource for the combobox or using Items.Add.

In the GetCellData event I have the following code:

Code:
Private Sub _VirtualTree_GetCellData(ByVal sender As Object, ByVal e As Infralution.Controls.VirtualTree.GetCellDataEventArgs) Handles _VirtualTree.GetCellData
    Dim ObjectRowBinding As Infralution.Controls.VirtualTree.ObjectRowBinding = New Infralution.Controls.VirtualTree.ObjectRowBinding
    Dim mComboboxControl As System.Windows.Forms.ComboBox

    ObjectRowBinding = _VirtualTree.GetRowBinding(e.Row)
    ObjectRowBinding.GetCellData(e.Row, e.Column, e.CellData)

      if (<this cell needs a combobox>) then
          mComboboxControl = New System.Windows.Forms.ComboBox
          mComboboxControl.DataSource = <a list of choises>
          mComboboxControl.Text = "some text"
          e.CellData.Editor = New CellEditor(mComboboxControl)
          e.CellData.Value = "some text"
      endif
  End Sub


This works fine, in the comboboxes in the tree I can see the list of values. But when I use:

Code:
          mComboboxControl.Items.Add(New String("one"))
          mComboboxControl.Items.Add(New String("two"))
          mComboboxControl.Items.Add(New String("three"))


instead of setting the Datasource, the list of items in the combobox is empty. I'm I forgetting something? Note that not all cells in the same column use the same items in their combobox.

Thanks,
Anja
Back to top
View user's profile Send private message Visit poster's website
Infralution



Joined: 28 Feb 2005
Posts: 5027

PostPosted: Tue Jun 10, 2008 10:46 pm    Post subject: Reply with quote

When Virtual Tree creates the actual controls used for editing it copies the properties of the template editor control using reflection. This can handle most simple properties like DataSource (where it can just copy the reference), however to copy complex properties like the ComboBox.Items property (which does not have set property accessor) is beyond its capabilities. If you want to initialize these properties then you need to handle the CellEditor.InitializeControl event as discussed previously.
_________________
Infralution Support
Back to top
View user's profile Send private message Visit poster's website
AB



Joined: 20 Nov 2007
Posts: 23
Location: Netherlands

PostPosted: Wed Jun 11, 2008 8:31 am    Post subject: Reply with quote

In other posts I read that it is not the best way to create a new cellEditor and control in the GetCellData event. It should be done at design time or in a constructor. But in my virtual tree I use a combobox with different items for different cells. Do I need to create one combobox (template) and reuse this one for all my cells, and can I change the items in the CellEditor.InitializeControl?

Another problem is that when I have two items in the combobox list, and the user types another value in the combobox (which is not in the list), this value must still be in the textfield part of the combobox when the user opens the combobox list to select another value. Now at that moment the first value from the combobox-list is automatically entered in the textfield part. Is this also something I can change in the CellEditor.InitializeControl?

Thanks a lot for your help,
Anja
Back to top
View user's profile Send private message Visit poster's website
Infralution



Joined: 28 Feb 2005
Posts: 5027

PostPosted: Wed Jun 11, 2008 10:26 pm    Post subject: Reply with quote

Quote:
Do I need to create one combobox (template) and reuse this one for all my cells, and can I change the items in the CellEditor.InitializeControl?


That is the best (for performance) approach. You can change the items in the ComboBox in the InitializeControl event handler.

Quote:
Now at that moment the first value from the combobox-list is automatically entered in the textfield part. Is this also something I can change in the CellEditor.InitializeControl?


The text in the combo-box will initially be set to the current value in the cell (even if the value is not in the drop down list) - is that what you mean?
_________________
Infralution Support
Back to top
View user's profile Send private message Visit poster's website
AB



Joined: 20 Nov 2007
Posts: 23
Location: Netherlands

PostPosted: Mon Jun 16, 2008 9:49 am    Post subject: Reply with quote

In the InitializeControl event handler I change the items, and in de GetCellData event I set the value for the cell, and everything works fine now. Thanks a lot for your support!

Anja
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Infralution Support Forum Index -> Virtual Tree Support All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group