开发者

How I read databindings for an Excel ListObject

I have a VS 2010 Excel 2007 Workbook application. I have a ListObject that is bound to an object data source via a binding source. I want to be able to determine what property of my object any given ListColumn obect in my ListObject's ListColumns collection is bound to. In the example below I use the column name to find the column that is bound to the "Field1" property. However, in my situation, the column name can be different from the property Name. There is no DataMember, DataPropertyName, or similar property on a ListColumn开发者_C百科 object, how do I figure out what column is bound to what property?

Given the class and ListObject below, I want to be able to use the following code:



return FindColumn(MyDataListObject, "Property1")




Public Class MyData
    Public Property Field1 As String
    Public Property Field2 As Date
End Class

   Public Function FindColumn(ByVal listObject As ListObject, 
ByVal propertyName As String) As ListColumn
        For Each col As ListColumn In listObject.ListColumns
            If col.Name = propertyName Then
                Return col
            End If
        Next
        Return Nothing
    End Function


I have a similar situation and I need figure bound columns on Excel.

Unfortunally, The VSTO hide the fields where the information is stored.

The only way to access this information by System.Reflection.

See below the small example:

    public static string[] GetListObjectMappedColumns(ListObject listObject)
    {
        Tools.ListObject vstoListObject = Globals.Factory.GetVstoObject(listObject);
        FieldInfo fieldInfo = vstoListObject.GetType().GetField("mappedProperties", BindingFlags.Instance | BindingFlags.NonPublic);
        object value = fieldInfo.GetValue(vstoListObject);
        if (value is PropertyDescriptor[])
        {
            List<string> result = new List<string>();
            foreach (PropertyDescriptor propertyDescriptor in (PropertyDescriptor[])value)
                result.Add(propertyDescriptor.Name);
            return result.ToArray();
        }
        return null;
    }

I hope help you !

Teobaldo

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜