Home Forums WinForms controls Xceed Grid for WinForms Binding complex types to grid

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

    I bind a List of an own complex type (in the example: Customer) to the grid. This type has also a property of another complex type (in the example: CustomerStatus). By overwriting the ToString method of this complex type the correct information is shown in the grid. Now I want to add a ComboBox to this column of complex type. And here is my problem. That doesn’t work, that means no ComboBox is shown. I don’t know why. Please help.

    ——————————————————————–

    public Form1()
    {

    List<Customer> liCustomers = new List<Customer>();
    // here I fill the List of Customers

    gc.BeginInit();
    gc.DataSource = liCustomers;
    Xceed.Grid.Editors.ComboBoxEditor cmb = new Xceed.Grid.Editors.ComboBoxEditor(liStatus, “”, “Title”);
    cmb.DisplayFormat = “%Title%”;
    gc.Columns[“Status”].CellEditorManager = cmb;
    gc.EndInit();
    }

    //The customer class:
    public class Customer
    {
    private string m_sFirstName;
    private string m_sLastName;
    //private string m_sStatus;
    private CustomerStatus m_csStatus;

    public string FirstName
    {
    get { return m_sFirstName; }
    set { m_sFirstName = value; }
    }

    public string LastName
    {
    get { return m_sLastName; }
    set { m_sLastName = value; }
    }

    public CustomerStatus Status
    {
    get { return m_csStatus; }
    set { m_csStatus = value; }
    }
    }

    //The customer status class:
    public class CustomerStatus
    {
    private int m_iRank;
    private string m_sTitle;

    public int Rank
    {
    get { return m_iRank; }
    set { m_iRank = value; }
    }

    public string Title
    {
    get { return m_sTitle; }
    set { m_sTitle = value; }
    }

    public override string ToString()
    {
    return this.Title;
    }
    }

    Imported from legacy forums. Posted by cmeyer1984 (had 2699 views)

    User (Old forums)
    Member
    Post count: 23064

    The editor does not display because there is an exception being thrown when entering in edition. The problem is that the column is of type CustomerStatus but the ValueMember of the ComboBoxEditor is a string field (“Title”).

    Thus the grid cannot convert type CustomerStatus to type String in SetContolValue method.

    You basically have 2 solution :

    1- The column containing CustomerStatus type could contain instead a primary key of the CustomerStatus. You could then assign a ComboBoxEditor and a ComboBoxViewer to the CellEditor/ViewerManager, having as DataSource a cross-list of the two field (ID and CustomerStatus). The ValueMember would be the ID, and the DisplayFormat would be the CustomerStatus.

    2- You could keep doing what you do, and make the grid display the CustomerStatus by using the ToString() method. You would need to derive from ComboBoxEditor, and override GetControlValue and SetContolValue to correctly go to and from one type and the other.

    We recommend the first solution.

    Imported from legacy forums. Posted by André (had 318 views)

    User (Old forums)
    Member
    Post count: 23064

    I solve the problem with the second suggestion. Here is my code, probably it is helpfully for other users with the same problem…

    //The Form
    public Form1()
    {

    List<Customer> liCustomers = new List<Customer>();
    // here I fill the List of Customers

    gc.BeginInit();
    gc.DataSource = liCustomers;

    MyComboBoxEditor cmb = new MyComboBoxEditor(liStatus, “Title”);
    cmb.DisplayFormat = “%Title%”;
    gc.Columns[“Status”].CellEditorManager = cmb;

    gc.EndInit();
    }

    //The derived ComboBoxEditor
    public class MyComboBoxEditor : Xceed.Grid.Editors.ComboBoxEditor
    {
    public MyComboBoxEditor(object DataSource, string ValueMember) : base(DataSource, “”, ValueMember) { }

    protected override object GetControlValueCore(System.Windows.Forms.Control control, Xceed.Grid.Cell cell, Type returnDataType)
    {
    Xceed.Editors.WinComboBox cmb = (Xceed.Editors.WinComboBox)control;
    return ((IListSource)cmb.DataSource).GetList()[cmb.SelectedIndex];
    }

    protected override void SetControlValueCore(System.Windows.Forms.Control control, Xceed.Grid.Cell cell)
    {
    Xceed.Editors.WinComboBox cmb = (Xceed.Editors.WinComboBox)control;
    object value = cell.Value;

    if (value == null)
    {
    cmb.SelectedItem = null;
    }
    else
    {
    cmb.SelectedValue = value.ToString();
    }
    }
    }

    Imported from legacy forums. Posted by cmeyer1984 (had 3887 views)

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