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

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)