IRowProvider 接口

请注意:此 API 现在已过时。

使 Web 部件到实现IRowConsumerICellConsumerIFilterConsumerIParametersInConsumer接口的 Web 部件发送的数据行。

命名空间:  Microsoft.SharePoint.WebPartPages.Communication
程序集:  Microsoft.SharePoint(位于 Microsoft.SharePoint.dll 中)

语法

声明
<ObsoleteAttribute("Use System.Web.UI.WebControls.WebParts.IWebPartRow instead")> _
Public Interface IRowProvider
用法
Dim instance As IRowProvider
[ObsoleteAttribute("Use System.Web.UI.WebControls.WebParts.IWebPartRow instead")]
public interface IRowProvider

备注

需要为的行,如表中的行传递一组可以具有特征的数据的 Web 部件,应实现IRowProvider接口。它可在方案中的使用者 Web 部件旨在与服务提供商发送数据架构的了解。此外,实现IRowProvider接口的 Web 部件有能力将初始化参数传递到使用者部件。IRowProvider接口可以连接到与任何其他连接接口的更多接口。它可以连接到ICellConsumer、 IRowConsumer、 IFilterConsumer和IParametersInConsumer接口。此外, IRowProvider接口的服务器端实现可连接到 Web 部件的不同页面上使用Microsoft SharePoint Foundation– 兼容的网页编辑器如Microsoft SharePoint Designer。连接到IRowConsumer接口的IRowProvider接口是直接连接,因此会不显示任何转换器对话框。当连接到ICellConsumer、 IFilterConsumer或IParametersInConsumer界面IRowProvider接口,转换器将显示对话框,允许最终用户的部分之间的行值映射。在这种情况下,使用部件不需要设计了数据架构,因为最终用户执行映射所发送的提供程序 Web 部件的知识。但是,这确实需要具有更深入地了解 Web 部件的功能的用户。

示例

下面的代码示例显示简单的 Web 部件可实现IRowProvider接口仅限于出的服务器运行的连接。它可以与一个或多个用于在服务器上实现ICellConsumer、 IRowConsumer、 IFilterConsumer或IParametersInConsumer接口的 Web 部件连接。本示例显示与每个行选择按钮的产品的列表。单击按钮时,此 Web 部件将该行将传递给所有其他 Web 部件连接到它。

特定于使其可连接的 Web 部件是 9 个步骤。以下步骤进行编号和代码示例中的注释。

创建可连接的 Web 部件的各个步骤的概述,请参阅Creating a Connectable Web Part。

' Common .NET required namespaces
Imports System
Imports System.ComponentModel
Imports System.Web.UI

' Web Part required namespaces
Imports Microsoft.SharePoint.WebPartPages
Imports System.Xml.Serialization
Imports System.Web.UI.WebControls

' DataGrid and UI namespaces
Imports System.Data
Imports System.Drawing

' Code Access Security namespaces
Imports System.Security
Imports Microsoft.SharePoint.Utilities

'Step #1: Make a reference to the Communication namespace.
Imports Microsoft.SharePoint.WebPartPages.Communication

Namespace ConnectionCodeSamples
   ' Step #2: Inherit from the WebPart base class and implement the 
   ' IRowProvider interface.
   
   Public Class ServerSideRowProvider
      Inherits WebPart
      Implements IRowProvider

      ' Step #3: Declare the IRowProvider events.
      ' Because this class implements the IRowProvider interface, it 
      ' must declare the interface members RowProviderInit and 
      ' RowReady. 
      Public Event RowProviderInit As RowProviderInitEventHandler Implements IRowProvider.RowProviderInit
      Public Event RowReady As RowReadyEventHandler Implements IRowProvider.RowReady
      
      ' Declare variables for keeping track of the connection state.
      Private _connected As Boolean = False
      Private _connectedWebPartTitle As String = String.Empty
      Private _registrationErrorMsg As String = "An error has occurred trying to register your connection interfaces."
      Private _registrationErrorOccurred As Boolean = False
      Private _notConnectedMsg As String = "NOT CONNECTED. To use this Web Part connect it to a Cell, Row or ParamsIn Consumer Web Part."
      
      ' Declare variables for the Web Part user interface.
      Private _connectedWebPartLabel As String = "Connected to Web Part"
      Private _dataGrid As New DataGrid()
      
      ' Declare variables for row information.
      Private _rowFieldNames() As String
      Private _rowFieldDisplayNames() As String
      
      '/ Step #4: Override the EnsureInterfaces() method and call the 
      ' RegisterInterface method.
      Public Overrides Sub EnsureInterfaces()
         ' If your Web Part is installed in the bin directory and the 
         ' Code Access Security (CAS) setting doesn't 
         ' allow Web Part Connections, an exception will be thrown. To 
         ' allow your Web Part to behave 
         ' well and continue working, a try/catch block should be used 
         ' when attempting to register interfaces.
         ' Web Part Connections will only work if the level attribute 
         ' of the <trust> tag in the 
         ' web.config file is set to WSS_Minimal, WSS_Medium, or Full. 
         ' By default a new SharePoint site
         ' is installed with the trust level set to WSS_Minimal.
         Try
            ' Register the IRowProvider interface.
            ' <param name="interfaceName">Friendly name of the 
            ' interface that is being implemented.</param>
            ' <param name="interfaceType">Specifies which interface is 
            ' being implemented.</param>
            ' <param name="maxConnections">Defines how many times this 
            ' interface can be connected.</param>
            ' <param name="runAtOptions">Determines where the interface 
            ' can run.</param>
            ' <param name="interfaceObject">Reference to the object 
            ' that is implementing this interface.</param>
            ' <param name="interfaceClientReference">Name used to 
            ' reference the interface on the client. 
            ' This is a server side example so the value is set to an 
            ' empty string.</param>
            ' <param name="menuLabel">Label for the interface that 
            ' appears in the UI</param>
            ' <param name="description">Description of the interface 
            ' that appears in the UI</param>
            ' <param name="allowCrossPageConnection">Specifies if the 
            ' interface can connect to a Web Part
            ' on a different page. This is an optional parameter with a 
            ' default of false. Note that only some 
            ' server side interfaces are allowed to connect across 
            ' pages by the Web Part infrastructure. 
            ' The IRowProvider interface is allowed to connect across 
            ' pages.</param>
            RegisterInterface("MyRowProviderInterface", InterfaceTypes.IRowProvider, WebPart.UnlimitedConnections, ConnectionRunAt.Server, Me, "", "Provide Row To", "Provides a row to a consumer Web Part.", True)

         Catch se As SecurityException
            _registrationErrorOccurred = True
         End Try
      End Sub
      
      ' Step #5: Override the CanRunAt() method.
      ' The CanRunAt method is called by the Web Part infrastructure during 
      ' the ASP.NET PreRender event
      ' to determine where the Web Part can run based on its current 
      ' configuration.
      Public Overrides Function CanRunAt() As ConnectionRunAt
         ' This Web Part can run on the server.
         Return ConnectionRunAt.Server
      End Function
      
      ' Step #6: Override the PartCommunicationConnect() method.
      ' PartCommunicationConnect() is called by the Web Part 
      ' Infrastructure to notify the Web Part that it
      ' is connected during the ASP.NET PreRender phase. Relevant 
      ' information is passed to the part such as 
      ' the interface it is connected over, the Web Part it is being 
      ' conected to, and where the part will be running, 
      ' either client or server side. 
      ' <param name="interfaceName">Friendly name of the interface that 
      ' is being connected</param>
      ' <param name="connectedPart">Reference to the other Web Part 
      ' that is being connected to</param>
      ' <param name="connectedInterfaceName">Friendly name of the 
      ' interface on the other Web Part</param>
      ' <param name="runAt">Where the interface should execute</param>
      Public Overrides Sub PartCommunicationConnect(interfaceName As String, connectedPart As WebPart, connectedInterfaceName As String, runAt As ConnectionRunAt)
         ' Keep track of whether the Web Part is connected or not.
         If interfaceName = "MyRowProviderInterface" Then
            _connected = True
            _connectedWebPartTitle = SPEncode.HtmlEncode(connectedPart.Title)
         End If
      End Sub
      
      ' Step #7: Override the PartCommunicationInit() method.
      ' The PartCommunicationInit method is called by the Web Part 
      ' infrastructure during the ASP.NET PreRender 
      ' event to allow the part to pass initialization information to 
      ' the other connected parts.
      ' It is important to always pass initialization information. Some 
      ' parts may not behave properly if this initialization 
      ' information is not received.
      Public Overrides Sub PartCommunicationInit()
            ' Ensure that all of the Web Part's controls are created.
            EnsureChildControls()

            ' Check if connected.
            If _connected Then
                ' Create the RowProviderInitEventArgs object for the 
                ' RowProviderInit event.
                Dim rowProviderInitEventArgs As New RowProviderInitEventArgs()

                ' Set the row field names.
                rowProviderInitEventArgs.FieldList = _rowFieldNames

                ' Set the row field display names.
                rowProviderInitEventArgs.FieldDisplayList = _rowFieldDisplayNames

                ' Fire the RowProviderInit event.
                RaiseEvent RowProviderInit(Me, rowProviderInitEventArgs)
            End If
        End Sub

      
      ' Step #8: Override the PartCommunicationMain method.
      ' The PartCommunicationMain method is called by the Web Part 
      ' infrastructure on the client during the 
      ' ASP.NET PreRender event to allow the part to pass its primary 
      ' data to the other connected parts.
      ' It is important to always fire the RowReady event. Some parts
      ' may not behave properly if they are left waiting for this 
      ' information.
       Public Overrides Sub PartCommunicationMain()
            ' Check if connected
            If _connected Then
                'Create the RowReadyEventArgs object for the RowReady 
                'event.
                Dim rowReadyEventArgs As New RowReadyEventArgs()

                ' Declare data variables.
                Dim selectionStatus As String = ""
                Dim dr(0) As DataRow

                ' If a row is selected, send the row.
                If _dataGrid.SelectedIndex > -1 Then
                    ' Generate an array containing the selected 
                    ' DataRow(s).
                    dr(0) = CType(_dataGrid.DataSource, DataTable).Rows(_dataGrid.SelectedIndex)

                    ' Set the selection status.
                    selectionStatus = "Standard"

                Else
                    ' The user hasn't selected a row so send null.
                    dr(0) = Nothing

                    ' Set selection status.
                    selectionStatus = "None"
                End If

                ' Set Values
                rowReadyEventArgs.Rows = dr
                rowReadyEventArgs.SelectionStatus = selectionStatus

                ' Fire the RowReady event.
                RaiseEvent RowReady(Me, rowReadyEventArgs)
            End If
        End Sub
      
      ' Step #9: Override the GetInitEventArgs() method.
      ' The GetInitEventArgs method is called by the Web Part 
      ' infrastructure during the ASP.NET PreRender  
      ' event to gather the necessary information it needs to build the 
      ' transformer dialog. The transformer dialog 
      ' is needed when connecting different interfaces such as 
      ' IRowProvider to ICellConsumer. The transformer dialog allows 
      ' the user to map the fields between the 
      ' interfaces. The GetInitEventArgs()method only needs to be 
      ' implemented for interfaces that
      ' can participate in a transformer which are the following:
      ' ICellConsumer, IRowProvider, IFilterConsumer, 
      ' IParametersOutProvider, IParametersInConsumer.
      ' <param name="interfacename">Name of interface on which the Web 
      ' Part infrastructure is requesting information</param>
      ' <returns>An InitEventArgs object</returns>
      Public Overrides Function GetInitEventArgs(interfaceName As String) As InitEventArgs
         ' Check if this is my particular row interface.
         If interfaceName = "MyRowProviderInterface" Then
            ' Ensure controls have been created.
            EnsureChildControls()
            
            ' Create the RowProviderInitEventArgs object for the 
            ' RowProviderInit event.
            Dim rowProviderInitEventArgs As New RowProviderInitEventArgs()
            
            ' Set the field names.
            rowProviderInitEventArgs.FieldList = _rowFieldNames
            
            ' Set the field display names.
            rowProviderInitEventArgs.FieldDisplayList = _rowFieldDisplayNames
            
            ' Return the RowProviderInitEventArgs object.
            Return rowProviderInitEventArgs
         Else
            Return Nothing
         End If
      End Function
      
      Protected Overrides Sub RenderWebPart(output As HtmlTextWriter)
         
         ' Check for connection interface registration error.
         If _registrationErrorOccurred Then
            output.Write(_registrationErrorMsg)
            Return
         End If
         
         ' Ensure that all of the Web Part's controls are created.
         EnsureChildControls()
         
         ' Check if connected.
         If _connected Then
            ' Line break.
            output.RenderBeginTag(HtmlTextWriterTag.Br)
            output.RenderEndTag()
            
            ' Render the DataGrid control.
            _dataGrid.RenderControl(output)
            
            ' Line break.
            output.RenderBeginTag(HtmlTextWriterTag.Br)
            output.RenderEndTag()
            
            ' Render the connected Web Part title.
            output.Write((_connectedWebPartLabel + ": "))
            output.RenderBeginTag(HtmlTextWriterTag.I)
            output.Write(_connectedWebPartTitle)
            output.RenderEndTag()
         
         Else
            ' The Web Part isn't connected.
            output.Write(_notConnectedMsg)
         End If
      End Sub
       
      ' Create the Web Part user interface controls.
      Protected Overrides Sub CreateChildControls()
         
         ' Create the DataTable.
         Dim dataTable As New DataTable()
         
         ' Add four column objects to the table.
         Dim idColumn As New DataColumn()
         idColumn.DataType = System.Type.GetType("System.Int32")
         idColumn.ColumnName = "ID"
         idColumn.Caption = "ID"
         idColumn.AutoIncrement = True
         dataTable.Columns.Add(idColumn)
         
         Dim Product As New DataColumn()
         Product.DataType = System.Type.GetType("System.String")
         Product.ColumnName = "Product"
         Product.Caption = "Product"
         dataTable.Columns.Add(Product)
         
         Dim productCategory As New DataColumn()
         productCategory.DataType = System.Type.GetType("System.String")
         productCategory.ColumnName = "Category"
         productCategory.Caption = "Category"
         dataTable.Columns.Add(productCategory)
         
         Dim Stock As New DataColumn()
         Stock.DataType = System.Type.GetType("System.Int32")
         Stock.ColumnName = "Stock"
         Stock.Caption = "Stock"
         dataTable.Columns.Add(Stock)
         
         ' Once a table has been created, use the NewRow method to 
         ' create a DataRow.
         Dim dataRow As DataRow
         
         ' Add the first row to the collection.
         dataRow = dataTable.NewRow()
         dataRow("Product") = "Aniseed Syrup"
         dataRow("Category") = "Condiments"
         dataRow("Stock") = 25
         dataTable.Rows.Add(dataRow)
         
         ' Add a second row.
         dataRow = dataTable.NewRow()
         dataRow("Product") = "Vegie-spread"
         dataRow("Category") = "Condiments"
         dataRow("Stock") = 10
         dataTable.Rows.Add(dataRow)
         
         ' Add a third row.
         dataRow = dataTable.NewRow()
         dataRow("Product") = "Outback Lager"
         dataRow("Category") = "Beverages"
         dataRow("Stock") = 30
         dataTable.Rows.Add(dataRow)
         
         ' Add a fourth row.
         dataRow = dataTable.NewRow()
         dataRow("Product") = "Boston Crab Meat"
         dataRow("Category") = "Seafood"
         dataRow("Stock") = 10
         dataTable.Rows.Add(dataRow)
         
         ' Add a fifth row.
         dataRow = dataTable.NewRow()
         dataRow("Product") = "Tofu"
         dataRow("Category") = "Produce"
         dataRow("Stock") = 41
         dataTable.Rows.Add(dataRow)
         
         ' Set the DataGrid's DataSource property.
         _dataGrid.DataSource = dataTable
         _dataGrid.ID = "DataGrid"
         
         ' Create the Selection column.
         Dim selectionColumn As New ButtonColumn()
         selectionColumn.ButtonType = ButtonColumnType.PushButton
         selectionColumn.CommandName = "Select"
         _dataGrid.Columns.Add(selectionColumn)
         
         ' Format the Data Grid.
         _dataGrid.HeaderStyle.Font.Bold = True
         _dataGrid.HeaderStyle.ForeColor = Color.DarkBlue
         _dataGrid.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
         _dataGrid.AlternatingItemStyle.BackColor = Color.Beige
         _dataGrid.SelectedItemStyle.BackColor = Color.Yellow
         
         _dataGrid.DataBind()
         Controls.Add(_dataGrid)
         
         ' Set the DataTable field name information.
         'This information will be passed to the Consumer by firing the 
         ' RowProviderInit event.
         Dim columnCount As Integer = dataTable.Columns.Count
         _rowFieldNames = New String(columnCount-1) {}
         _rowFieldDisplayNames = New String(columnCount-1) {}
         
         Dim i As Integer
         For i = 0 To columnCount - 1
            _rowFieldNames(i) = dataTable.Columns(i).ColumnName
            _rowFieldDisplayNames(i) = dataTable.Columns(i).Caption
         Next i
      End Sub
   End Class
End Namespace
// Common .NET required namespaces
using System;
using System.ComponentModel;
using System.Web.UI;

// Web Part required namespaces
using Microsoft.SharePoint.WebPartPages;
using System.Xml.Serialization;
using System.Web.UI.WebControls;

// DataGrid and UI namespaces
using System.Data;
using System.Drawing;

// Code Access Security namespaces
using System.Security;
using Microsoft.SharePoint.Utilities;

//Step #1: Make a reference to the Communication namespace.
using Microsoft.SharePoint.WebPartPages.Communication;


namespace ConnectionCodeSamples
{
    // Step #2: Inherit from the WebPart base class and implement the 
    // IRowProvider interface.
    public class ServerSideRowProvider : WebPart, IRowProvider
    {    
        // Step #3: Declare the IRowProvider events.
        // Because this class implements the IRowProvider interface, it 
        // must declare the interface members RowProviderInit and 
        // RowReady. 
        public event RowProviderInitEventHandler RowProviderInit;
        public event RowReadyEventHandler RowReady;                

        // Declare variables for keeping track of connection state.
        private bool _connected = false;
        private string _connectedWebPartTitle = string.Empty;
        private string _registrationErrorMsg = "An error has occurred trying to register your connection interfaces.";
        private bool _registrationErrorOccurred = false;
        private string _notConnectedMsg = "NOT CONNECTED. To use this Web Part connect it to a Cell, Row or ParamsIn Consumer Web Part.";

        // Declare variables for Web Part user interface.
        private string _connectedWebPartLabel = "Connected to Web Part";    
        private DataGrid _dataGrid = new DataGrid();
        
        // Declare variables for row information.
        private string[] _rowFieldNames;
        private string[] _rowFieldDisplayNames;
        

        /// Step #4: Override the EnsureInterfaces() method and call 
        /// the RegisterInterface method.
        public override void EnsureInterfaces()
        {
            // If your Web Part is installed in the bin directory and 
            // the Code Access Security (CAS) setting doesn't 
            // allow Web Part Connections, an exception will be thrown. 
            // To allow your Web Part to behave 
            // well and continue working, a try/catch block should be 
            // used when attempting to register interfaces.
            // Web Part Connections will only work if the level 
            // attribute of the <trust> tag in the 
            // web.config file is set to WSS_Minimal, WSS_Medium, or 
            // Full. By default a new SharePoint site
            // is installed with the trust level set to WSS_Minimal.
            try
            {
                // Register the IRowProvider interface.
                // <param name="interfaceName">Friendly name of the 
                // interface that is being implemented.</param>
                // <param name="interfaceType">Specifies which 
                // interface is being implemented.</param>
                // <param name="maxConnections">Defines how many times 
                // this interface can be connected.</param>
                // <param name="runAtOptions">Determines where the 
                // interface can run.</param>
                // <param name="interfaceObject">Reference to the 
                // object that is implementing this interface.</param>
                // <param name="interfaceClientReference">Name used to 
                // reference the interface on the client. 
                // This is a server side example so the value is set to 
                // empty string.</param>
                // <param name="menuLabel">Label for the interface 
                // that appears in the UI</param>
                // <param name="description">Description of the 
                // interface that appears in the UI</param>
                // <param name="allowCrossPageConnection">Specifies if 
                // the interface can connect to a Web Part
                // on a different page. This is an optional parameter 
                // with a default of false. Note that only some 
                // server side interfaces are allowed to connect across 
                // pages by the Web Part infrastructure. 
                // The IRowProvider interface is allowed to connect 
                // across pages.</param>
                RegisterInterface("MyRowProviderInterface",                 //InterfaceName    
                    InterfaceTypes.IRowProvider,                            //InterfaceType
                    WebPart.UnlimitedConnections,                           //MaxConnections
                    ConnectionRunAt.Server,                                 //RunAtOptions
                    this,                                                   //InterfaceObject
                    "",                                                     //InterfaceClientReference
                    "Provide Row To",                                       //MenuLabel
                    "Provides a row to a consumer Web Part.",               //Description
                    true);                                                  //allowCrossPageConnection
            }
            catch(SecurityException se)
            {
                _registrationErrorOccurred = true;
            }
        }



        // Step #5: Override the CanRunAt() method.
        // The CanRunAt method is called by the Web Part infrastructure 
        // during the ASP.NET PreRender event
        // to determine where the Web Part can run based on its current 
        // configuration.
        public override ConnectionRunAt CanRunAt()
        {
            // This Web Part can run on the server.
            return ConnectionRunAt.Server;
        }

        // Step #6: Override the PartCommunicationConnect() method.
        // PartCommunicationConnect() is called by the Web Part 
        // Infrastructure to notify the Web Part that it
        // is connected during the ASP.NET PreRender phase. Relevant 
        // information is passed to the part such as 
        // the interface it is connected over, the Web Part it is being 
        // conected to, and where the part will be running, 
        // either client or server side. 
        // <param name="interfaceName">Friendly name of the interface 
        // that is being connected</param>
        // <param name="connectedPart">Reference to the other Web Part 
        // that is being connected to</param>
        // <param name="connectedInterfaceName">Friendly name of the 
        // interface on the other Web Part</param>
        // <param name="runAt">Where the interface should 
        // execute</param>
        public override void PartCommunicationConnect(
            string interfaceName,
            WebPart connectedPart,
            string connectedInterfaceName,
            ConnectionRunAt runAt)
        {
            // Keep track of whether the Web Part is connected or not.
            if (interfaceName == "MyRowProviderInterface")
            {
                _connected = true;
                _connectedWebPartTitle = SPEncode.HtmlEncode(connectedPart.Title);
            }
        }

        // Step #7: Override the PartCommunicationInit() method.
        // The PartCommunicationInit method is called by the Web Part 
        // infrastructure during the ASP.NET PreRender 
        // event to allow the part to pass initialization information 
        // to the other connected parts.
        // It is important to always pass initialization information. 
        // Some parts may not behave properly if this initialization 
        // information is not received.
        public override void PartCommunicationInit()
        {
            // Ensure that all of the Web Part's controls are created.
            EnsureChildControls();

            // Check if connected.
            if(_connected)
            {
                // If there is a listener, fire the RowProviderInit event.
                if (RowProviderInit != null)
                {
                    // Create the RowProviderInitEventArgs object for 
                    // the RowProviderInit event.
                    RowProviderInitEventArgs rowProviderInitEventArgs = new RowProviderInitEventArgs();
                    
                    // Set the row field names.
                    rowProviderInitEventArgs.FieldList = _rowFieldNames;

                    // Set the row field display names.
                    rowProviderInitEventArgs.FieldDisplayList = _rowFieldDisplayNames;

                    // Fire the RowProviderInit event.
                    RowProviderInit(this, rowProviderInitEventArgs);
                }
            }
        }


        // Step #8: Override the PartCommunicationMain method.
        // The PartCommunicationMain method is called by the Web Part 
        // infrastructure on the client during the 
        // ASP.NET PreRender event to allow the part to pass its 
        // primary data to the other connected parts.
        // It is important to always fire the RowReady event. Some 
        // parts may not behave properly if they are left waiting for 
        // this information.
        public override void PartCommunicationMain()
        {
            // Check if connected
            if(_connected)
            {
                //If there is a listener, fire the RowReady event.
                if (RowReady != null)
                {
                    //Create the RowReadyEventArgs object for the 
                    //RowReady event.
                    RowReadyEventArgs rowReadyEventArgs = new RowReadyEventArgs();

                    // Declare data variables.
                    string selectionStatus = "";
                    DataRow[] dr = new DataRow[1];

                    // If a row is selected, send the row.
                    if ( _dataGrid.SelectedIndex > -1)
                    {
                        // Generate an array containing the selected 
                        // DataRow(s).
                        dr[0] = ((DataTable)_dataGrid.DataSource).Rows[_dataGrid.SelectedIndex];
                    
                        // Set the selection status.
                        selectionStatus = "Standard";

                    }
                    else
                    {
                        // The user hasn't selected a row so send null.
                        dr[0] = null;

                        // Set selection status.
                        selectionStatus = "None";
                    }

                    // Set values
                    rowReadyEventArgs.Rows = dr;
                    rowReadyEventArgs.SelectionStatus = selectionStatus;

                    // Fire the RowReady event.
                    RowReady(this, rowReadyEventArgs);
                }
            }
        }


        // Step #9: Override the GetInitEventArgs() method.
        // The GetInitEventArgs method is called by the Web Part 
        // infrastructure during the ASP.NET PreRender  
        // event to gather the necessary information it needs to build 
        // the transformer dialog. The transformer dialog 
        // is needed when connecting different interfaces such as 
        // IRowProvider to ICellConsumer. The transformer dialog allows 
        // the user to map the fields between the 
        // interfaces. The GetInitEventArgs()method only needs to be 
        // implemented for interfaces that
        // can participate in a transformer which are the following:
        // ICellConsumer, IRowProvider, IFilterConsumer, 
        // IParametersOutProvider, IParametersInConsumer.
        // <param name="interfacename">Name of interface on which the 
        // Web Part infrastructure is requesting information</param>
        // <returns>An InitEventArgs object</returns>
        public override InitEventArgs GetInitEventArgs(string interfaceName)
        {
            // Check if this is my particular row interface.
            if (interfaceName == "MyRowProviderInterface")
            {
                // Ensure controls have been created.
                EnsureChildControls();

                // Create the RowProviderInitEventArgs object for the 
                // RowProviderInit event.
                RowProviderInitEventArgs rowProviderInitEventArgs = new RowProviderInitEventArgs();
                    
                // Set the field names.
                rowProviderInitEventArgs.FieldList = _rowFieldNames;

                // Set the field display names.
                rowProviderInitEventArgs.FieldDisplayList = _rowFieldDisplayNames;
                    
                // return the RowProviderInitEventArgs object.
                return(rowProviderInitEventArgs);
            }
            else
            {
                return(null);
            }
        }

        protected override void RenderWebPart(HtmlTextWriter output)
        {
        
            // Check for connection interface registration error.
            if (_registrationErrorOccurred)
            {
                output.Write(_registrationErrorMsg);
                return;
            }

            // Ensure that all of the Web Part's controls are created.
            EnsureChildControls();

            // Check if connected.
            if(_connected)
            {
                // Line break.
                output.RenderBeginTag(HtmlTextWriterTag.Br);
                output.RenderEndTag();

                // Render the DataGrid control.
                _dataGrid.RenderControl(output);

                // Line break.
                output.RenderBeginTag(HtmlTextWriterTag.Br);
                output.RenderEndTag();

                // Render connected Web Part title.
                output.Write(_connectedWebPartLabel + ": ");
                output.RenderBeginTag(HtmlTextWriterTag.I);
                output.Write(_connectedWebPartTitle);
                output.RenderEndTag();

            }
            else
            {
                // The Web Part isn't connected.
                output.Write(_notConnectedMsg);
            }

        }

        // Create the Web Part user interface controls.
        protected override void CreateChildControls()
        {
            
            // Create the DataTable.
            DataTable dataTable = new DataTable();

            // Add four column objects to the table.
            DataColumn idColumn = new  DataColumn();
            idColumn.DataType = System.Type.GetType("System.Int32");
            idColumn.ColumnName = "ID";
            idColumn.Caption = "ID";
            idColumn.AutoIncrement = true;
            dataTable.Columns.Add(idColumn);

            DataColumn Product = new DataColumn();
            Product.DataType = System.Type.GetType("System.String");
            Product.ColumnName = "Product";
            Product.Caption = "Product";
            dataTable.Columns.Add(Product);

            DataColumn productCategory = new DataColumn();
            productCategory.DataType = System.Type.GetType("System.String");
            productCategory.ColumnName = "Category";
            productCategory.Caption = "Category";
            dataTable.Columns.Add(productCategory);

            DataColumn Stock = new DataColumn();
            Stock.DataType = System.Type.GetType("System.Int32");
            Stock.ColumnName = "Stock";
            Stock.Caption = "Stock";
            dataTable.Columns.Add(Stock);

            // Once a table has been created, use the NewRow method to 
            // create a DataRow.
            DataRow dataRow;
            
            // Add the first row to the collection.
            dataRow = dataTable.NewRow();
            dataRow["Product"] = "Aniseed Syrup";
            dataRow["Category"] = "Condiments";
            dataRow["Stock"] = 25;
            dataTable.Rows.Add(dataRow);

            // Add a second row.
            dataRow = dataTable.NewRow();
            dataRow["Product"] = "Vegie-spread";
            dataRow["Category"] = "Condiments";
            dataRow["Stock"] = 10;
            dataTable.Rows.Add(dataRow);

            // Add a third row.
            dataRow = dataTable.NewRow();
            dataRow["Product"] = "Outback Lager";
            dataRow["Category"] = "Beverages";
            dataRow["Stock"] = 30;
            dataTable.Rows.Add(dataRow);

            // Add a fourth row.
            dataRow = dataTable.NewRow();
            dataRow["Product"] = "Boston Crab Meat";
            dataRow["Category"] = "Seafood";
            dataRow["Stock"] = 10;
            dataTable.Rows.Add(dataRow);

            // Add a fifth row.
            dataRow = dataTable.NewRow();
            dataRow["Product"] = "Tofu";
            dataRow["Category"] = "Produce";
            dataRow["Stock"] = 41;
            dataTable.Rows.Add(dataRow);

            // Set the DataGrid's DataSource property.
            _dataGrid.DataSource = dataTable;
            _dataGrid.ID = "DataGrid";

            // Create the Selection column.
            ButtonColumn selectionColumn = new ButtonColumn();
            selectionColumn.ButtonType = ButtonColumnType.PushButton;
            selectionColumn.CommandName = "Select";
            _dataGrid.Columns.Add(selectionColumn);
            

            // Format the Data Grid.
            _dataGrid.HeaderStyle.Font.Bold = true;
            _dataGrid.HeaderStyle.ForeColor = Color.DarkBlue;
            _dataGrid.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
            _dataGrid.AlternatingItemStyle.BackColor = Color.Beige;
            _dataGrid.SelectedItemStyle.BackColor = Color.Yellow;

            _dataGrid.DataBind();
            Controls.Add(_dataGrid);

            // Set the DataTable field name information.
            // This information will be passed to the Consumer by 
            // firing the RowProviderInit event.
            int columnCount = dataTable.Columns.Count;
            _rowFieldNames = new string[columnCount];
            _rowFieldDisplayNames = new string[columnCount];

            for(int i = 0; i < columnCount; i++)
            {
                _rowFieldNames[i] = dataTable.Columns[i].ColumnName;
                _rowFieldDisplayNames[i] = dataTable.Columns[i].Caption;
            }
        }
    }
}

另请参阅

引用

IRowProvider 成员

Microsoft.SharePoint.WebPartPages.Communication 命名空间