Home - Forums-.NET - FlyGrid.Net (Windows Forms) - Binding Custom DTO to DropDownList Cell

FlyGrid.Net (Windows Forms)

.NET Datagrid - Fast, highly customizable, industry standards .NET data grid control for WinForms

This forum related to following products: FlyGrid.Net

Binding Custom DTO to DropDownList Cell
Link Posted: 14-Jul-2006 02:16
This sounds like something very trivial and should be simple to do, but I cannot seem to get it to work.


I have a DTO in some library:


public class ActivityLevelDTO
{
    public ActivityLevelDTO() {}

    private int _id;
    private string _description;

    public int Id
    {
       get { return _id; }
    }

    public string Description
    {
       get { return _description; }
    }
}


Very simple.  In my code I have an array of this DTO and I want to bind the collection to a column in each row of data.  I am populating the grid with a DataTable of data that I build from information returned from a web service.  The DataTabel column that goes into the column where I want my DropDownList contains an Id that would exist in the array of ActivityLevelDTO.

This is my declaration of the FlyGrid column:


ClientActivityLevelDTO[] _levels = !! web service call !!

this.colActivityLevel = new NineRays.Windows.Forms.Grids.LookupListColumn();
this.colActivityLevel.Caption = \"Activity\";
this.colActivityLevel.DropDownStyle = NineRays.Windows.Forms.Grids.DropDownStyle.DropDownList;
this.colActivityLevel.FieldName = \"ActivityLevel\";
this.colActivityLevel.LookupBoundField = \"Id\";
this.colActivityLevel.LookupDataMember = \"Id\";
this.colActivityLevel.LookupDisplayField = \"Description\";

this.colActivityLevel.LookupSource = _levels



I then bind my DataTable to the grid rows DataSource and all that is displayed in the column is the int value of the DataTable value.  The DropDownList does not fill.  Any help would be greatly appreciated.
Link Posted: 17-Jul-2006 23:17
LookuplistColumn directly doesn't support binding to IList objects, but with the nearest FlyGrid update (planning to release today) you can use following solution:(see the next topic)
Here you can download a sample
Link Posted: 17-Jul-2006 23:20
[C#]
public class DTOLookupListColumn : LookupListColumn
{
  private object[] displayList = null;
  private object[] boundList = null;  
  private void ResetLists()
  {
    displayList=null;
    boundList=null;      
  }  
  public override string LookupDisplayField
  {
    set
    {
      base.LookupDisplayField = value;
      ResetLists();
    }
  }
  public override string LookupBoundField
  {
    set
    {
      base.LookupBoundField = value;
      ResetLists();
    }
  }
  public override object LookupSource
  {
    set
    {
      base.LookupSource = value;
      ResetLists();
    }
  }
  private void PrepareDisplayList()
  {
    Array alist = this.LookupSource as Array;
    if (alist != null)
    {
      object[] values = new object[alist.Length];
      for(int i=0; i < values.Length; i++)
      {
        object value = alist.GetValue(i);
        if (value != null)
        {                            
          PropertyDescriptorCollection lpdc = TypeDescriptor.GetProperties(value);
          PropertyDescriptor lpdLookupDisplayField = lpdLookupDisplayField = LookupDisplayField != null && LookupDisplayField != string.Empty ? lpdc[this.LookupDisplayField] : null;          
          if (lpdLookupDisplayField != null)
          {
            values[i] = lpdLookupDisplayField.GetValue(value);
          }
        }
      }
      displayList = values;
    }
  }
  private void PrepareBoundList()
  {
    Array alist = this.LookupSource as Array;
    if (alist != null)
    {
      object[] values = new object[alist.Length];
      for(int i=0; i < values.Length; i++)
      {
        object value = alist.GetValue(i);
        if (value != null)
        {                            
          PropertyDescriptorCollection lpdc = TypeDescriptor.GetProperties(value);
          PropertyDescriptor lpdLookupBoundField = lpdLookupBoundField = LookupBoundField != null && LookupBoundField != string.Empty ? lpdc[this.LookupBoundField] : null;          
          if (lpdLookupBoundField != null)
          {
            values[i] = lpdLookupBoundField.GetValue(value);
          }
        }
      }
      boundList = values;
    }
  }
  private object[] BoundList
  {
    get
    {
      if (boundList == null)
        PrepareBoundList();
      return boundList;
    }
  }
  private object[] DisplayList
  {
    get
    {
      if (displayList == null)
        PrepareDisplayList();
      return displayList;
    }
  }
  public override object[] GetValueList(NodeBase node)
  {
    return DisplayList != null ? DisplayList : base.GetValueList(node);
  }
  public override object TranslateToValue(object data)
  {
    if (DisplayList != null && BoundList != null)
    {
      int index = Array.IndexOf(DisplayList, data);
      if (index != -1)
        return BoundList.GetValue(index);
    }
    return base.TranslateToValue(data);
  }
  public override object TranslateValue(object data)
  {
    if (DisplayList != null && BoundList != null)
    {
      int index = Array.IndexOf(BoundList, data);
      if (index != -1)
        return DisplayList.GetValue(index);
    }
    return base.TranslateValue(data);
  }
}
Link Posted: 18-Jul-2006 21:02
[quote="NineRays"]LookuplistColumn directly doesn't support binding to IList objects, but with the nearest FlyGrid update (planning to release today) you can use following solution:(see the next topic)
Here you can download a sample


Hi, can you tell me which version number you are referring to?

thx.
Link Posted: 19-Jul-2006 00:11
Sorry for some delays with publishing the latest version of FlyGrid.Net,
I was referred to v1.4.0.19, this version is already available for downloading.
Link Posted: 19-Jul-2006 03:34
[quote="NineRays"]
[C#]
public class DTOLookupListColumn : LookupListColumn
{
  ...
  public override string LookupDisplayField
  {
    set
    {
      base.LookupDisplayField = value;
      ResetLists();
    }
  }
  public override string LookupBoundField
  {
    set
    {
      base.LookupBoundField = value;
      ResetLists();
    }
  }
  public override object LookupSource
  {
    set
    {
      base.LookupSource = value;
      ResetLists();
    }
  }
  ...
}


Are you sure this latest release (v1.4.0.19) supports this?  When I build the above overrides give me the following error:


DTOLookupListColumn.LookupDisplayField.set': cannot override inherited member 'NineRays.Windows.Forms.Grids.LookupListColumn.LookupDisplayField.set' because it is not marked virtual, abstract, or override


J.