SPDataSourceViewResultItem Class
Provides custom type information for result item objects that are returned by the SPDataSourceView class.
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: No
This class is used by developers of data-bound controls. It is not intended for use as a programmable object in page code.
The SPDataSourceViewResultItem class provides a wrapper for objects that are returned by an SPDataSourceView object as the result of a query. Result objects can include instances of the SPListItem class, the SPList class, and the SPWeb class. None of these data types implements the ICustomTypeDescriptor interface, but the interface is required for interaction with data-bound controls. Therefore, when an SPDataSourceView object returns data to a data-bound control, it wraps each result item in an instance of the SPDataSourceViewResultItem class, which does implement the ICustomTypeDescriptor interface. The actual result item can be accessed through the ResultItem property of the SPDataSourceViewResultItem object.
The Microsoft.SharePoint.WebControls.SPDataSourceViewResultItem class inherits from the System.ComponentModel.ICustomTypeDescriptor interface to provide object masking. ICustomTypeDescriptor provides for scenarios when object access occurs during reflection, such as when binding a SPGridView object DataSource property to a SPDataSource (current situation), ICustomTypeDescriptor allows the masking of objects for behaviors such as DataBinding. Through the use of SPDataSourceViewResultItem, a customized set of properties can be presented for Microsoft.SharePoint.WebControls.SPDataSourceView objects.
Usage Scenario
The heaviest representation of the SPDataSourceViewResultItem class is evident when accessing data in a SPGridView after DataBinding occurs with a DataSource type of SPDataSource. Such a programming technique is generally valuable when operations are targeted to manipulation of SPGridViewRow contents. When binding a SPGridView to a SPDataSource, the underlying SPGridViewRow is represented by a SPListItem object. Because the data can be exposed as this type, it can be leveraged as any orthodox SPListItem object.
In the below example, the CreateChildControls method is being overridden. Within CreateChildControls, two controls are being created, a new SPGridView object and a new SPDataSource object, the latter hydrating the SPGridView.DataSource property after hydrating with a list from the current web context. Since SPDataSourceViewResultItem is helpful for access post DataBinding, the DataRowBound event of the SPGridView object is being wired. Within the wired data bound event, the necessary casting in order to access the contents as a SPListItem object is demonstrated.
C# Code Example
public class MyClass : WebPart
{
protected override void CreateChildControls()
{
SPGridView view = new SPGridView();
SPDataSource source = new SPDataSource();
source.DataSourceMode = SPDataSourceMode.List;
source.List = SPContext.Current.Web.Lists["DataSource"];
view.DataSource = source;
view.RowDataBound += view_RowDataBound;
base.CreateChildControls();
}
private static void view_RowDataBound(object sender, GridViewRowEventArgs e)
{
SPDataSourceViewResultItem item = (SPDataSourceViewResultItem)e.Row.DataItem;
SPListItem listItem = (SPListItem)item.ResultItem;
}
}
VB.NET Code Example
Public Class [MyClass]
Inherits WebPart
Protected Overloads Overrides Sub CreateChildControls()
Dim view As New SPGridView()
Dim source As New SPDataSource()
source.DataSourceMode = SPDataSourceMode.List
source.List = SPContext.Current.Web.Lists("DataSource")
view.DataSource = source
view.RowDataBound += view_RowDataBound
MyBase.CreateChildControls()
End Sub
Private Shared Sub view_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
Dim item As SPDataSourceViewResultItem = DirectCast(e.Row.DataItem, SPDataSourceViewResultItem)
Dim listItem As SPListItem = DirectCast(item.ResultItem, SPListItem)
End Sub
End Class
Adam Buenz
SharePoint Foundation MVP - http://www.sharepointsecurity.com
- 6/3/2010
- Adam Buenz - MVP