2012-11-20 55 views
0

我想將HQL查詢結果與DevExpressGridControl綁定。將HQL查詢結果綁定到DevExpress GridControl

無法在編譯時預測查詢定義。它基於用戶定義的配置設置而構建。

var queryResults = GetSession().CreateQuery(PrepereHQLQuery()).List(); 
gridControl1.DataSource = queryResults; 

假設PrepareHQLQuery回報select col1, col2 from MyTable我希望得到的東西是這樣的:

enter image description here

取而代之的是:

I have no concept how to force DataGrid to show data from array of object

不存在任何方式來配置GridView控件列直接顯示來自的數據的Object[]

我也想避免中間對象,如DTO由於我的HQL查詢的不可預測性。

回答

1

您可以創建封裝類來封裝實現IList和ITypedList接口的HQL查詢結果。這種方法允許您使用HQL查詢結果作爲XtraGrid的數據源。
這裏有一個你可以適應的小樣本:

IList<object[]> queryResult = new List<object[]>{ 
    new object[]{ "a", 11 }, 
    new object[]{ "b", 22 } 
}; 
gridControl1.DataSource = new QueryWrapper(queryResult); 
//... 
public class QueryWrapper : IList, ITypedList { 
    class ColumnDescriptor : PropertyDescriptor { 
     int index; 
     Type elementType; 
     public ColumnDescriptor(string name, int index, Type elementType) 
      : base(name, null) { 
      this.index = index; 
      this.elementType = elementType; 
     } 
     public override Type ComponentType { 
      get { return typeof(RowDescriptor); } 
     } 
     public override bool IsReadOnly { 
      get { return false; } 
     } 
     public override Type PropertyType { 
      get { return elementType; } 
     } 
     public override object GetValue(object component) { 
      return ((RowDescriptor)component).GetValue(index); 
     } 
     public override void SetValue(object component, object value) { 
      ((RowDescriptor)component).SetValue(index, value); 
     } 
     public override bool CanResetValue(object component) { return false; } 
     public override void ResetValue(object component) { } 
     public override bool ShouldSerializeValue(object component) { return false; } 
    } 
    class RowDescriptor : CustomTypeDescriptor { 
     QueryWrapper owner; 
     object[] rowObjects; 
     public RowDescriptor(QueryWrapper owner, object[] rowObjects) { 
      this.rowObjects = rowObjects; 
      this.owner = owner; 
     } 
     public object GetValue(int index) { 
      return rowObjects[index]; 
     } 
     public void SetValue(int index, object value) { 
      rowObjects[index] = value; 
     } 
     public override PropertyDescriptorCollection GetProperties(Attribute[] attributes) { 
      return owner.pdc; 
     } 
    } 
    IList<object[]> query; 
    List<RowDescriptor> list; 
    public QueryWrapper(IList<object[]> query) { 
     this.query = query; 
     list = new List<RowDescriptor>(query.Count); 
     for(int i = 0; i < query.Count; i++) 
      list.Add(new RowDescriptor(this, query[i])); 
    } 
    #region IList Members 
    int IList.Add(object value) { 
     throw new NotSupportedException(); 
    } 
    void IList.Clear() { 
     throw new NotSupportedException(); 
    } 
    bool IList.Contains(object value) { 
     return value is RowDescriptor && list.Contains((RowDescriptor)value); 
    } 
    int IList.IndexOf(object value) { 
     return (value is RowDescriptor) ? list.IndexOf((RowDescriptor)value) : -1; 
    } 
    void IList.Insert(int index, object value) { 
     throw new NotSupportedException(); 
    } 
    bool IList.IsFixedSize { 
     get { return true; } 
    } 
    bool IList.IsReadOnly { 
     get { return true; } 
    } 
    void IList.Remove(object value) { 
     throw new NotSupportedException(); 
    } 
    void IList.RemoveAt(int index) { 
     throw new NotSupportedException(); 
    } 
    object IList.this[int index] { 
     get { return list[index]; } 
     set { throw new NotSupportedException(); } 
    } 
    #endregion 
    #region ICollection Members 
    void ICollection.CopyTo(Array array, int index) { 
     if(array is RowDescriptor[]) list.CopyTo((RowDescriptor[])array, index); 
    } 
    int ICollection.Count { 
     get { return list.Count; } 
    } 
    bool ICollection.IsSynchronized { 
     get { return false; } 
    } 
    object ICollection.SyncRoot { 
     get { return this; } 
    } 
    #endregion 
    #region IEnumerable Members 
    IEnumerator IEnumerable.GetEnumerator() { 
     return list.GetEnumerator(); 
    } 
    #endregion 
    #region ITypedList Members 
    PropertyDescriptorCollection pdc; 
    PropertyDescriptorCollection ITypedList.GetItemProperties(PropertyDescriptor[] listAccessors) { 
     if(pdc == null) { 
      if(query.Count > 0) { 
       PropertyDescriptor[] pd = new PropertyDescriptor[query[0].Length]; 
       for(int i = 0; i < pd.Length; i++) 
        pd[i] = new ColumnDescriptor("Column" + i, i, query[0][i].GetType()); 
       pdc = new PropertyDescriptorCollection(pd); 
      } 
      else pdc = new PropertyDescriptorCollection(new PropertyDescriptor[] { }); 
     } 
     return pdc; 
    } 
    string ITypedList.GetListName(PropertyDescriptor[] listAccessors) { return string.Empty; } 
    #endregion 
}