Some sample code that outlines how to implement the IWebPartTable interface can be found at: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.webparts.iwebparttable.aspx. It is however worth mentioning that we think that this sample implementation is not correct, because of the following reasons:
1. The Schema property getter of TableProviderWebPart returns a PropertyDescriptorCollection instance, that is constructed from the the first row of the DefaultView, while the GetTableData method returns the table data rows. You cannot access the values of the enumerated records with the returned property descriptors, because the property descriptors are for DataRowView instances, not for DataRow instances.
2. The TableConsumer explicitly casts the enumerated data table objects to DataRow, which is the only reason for this example to work. If the returned ICollection for the data table is not a collection of DataRow items this sample will simply throw an exception.
So following is some sample code that correctly implements the IWebPartTable interface.
Data Provider Web Part |
Copy Code
|
---|---|
public class MyWebPartTableWebPart : Microsoft.SharePoint.WebPartPages.WebPart, IWebPartTable { ... DataTable m_DataTable = null; void InitDataTable() { if (m_DataTable != null) return; m_DataTable = new DataTable(); m_DataTable.Columns.Add(new DataColumn("Company", typeof(String))); m_DataTable.Columns.Add(new DataColumn("Sales", typeof(Double))); m_DataTable.Rows.Add("Bigfoot", 124); m_DataTable.Rows.Add("Bigfoot", 234); m_DataTable.Rows.Add("Bigfoot", 782); m_DataTable.Rows.Add("Maison", 324); m_DataTable.Rows.Add("Maison", 534); m_DataTable.Rows.Add("Maison", 182); m_DataTable.Rows.Add("Leka", 524); m_DataTable.Rows.Add("Leka", 334); m_DataTable.Rows.Add("Leka", 282); } public void GetTableData(TableCallback callback) { InitDataTable(); callback(m_DataTable.DefaultView); } public PropertyDescriptorCollection Schema { get { InitDataTable(); return TypeDescriptor.GetProperties(m_DataTable.DefaultView[0]);<br> } } ... } |
The consumer of the web part data table on the other hand needs to only rely on the returned Schema and collection of objects as shown in the following code sample:
Data Consumer Web Part |
Copy Code
|
---|---|
// somehow get a reference to the web part table interface IWebPartTable webPart = ...; // get the table schema PropertyDescriptorCollection tableSchema = webPart.Schema; // iterate through the data table row values webPart.GetTableData(delegate(ICollection tableData) { foreach (object row in tableData) { for (int col = 0; col < tableSchema.Count; col++) { // get the value for the current row/col object value = tableSchema[col].GetValue(row); } } }); |
If the predefined set of available data connections is not fitting into your SharePoint application scenario you can contact Nevron Consulting at consulting@nevron.com for a price quote for a custom web part that connects to your data.