Compartilhar via


IRowProvider Interface

NOTE: This API is now obsolete.

Enables a Web Part to send a row of data to a Web Part that implements the IRowConsumer, ICellConsumer, IFilterConsumer, or IParametersInConsumer interface.

Namespace:  Microsoft.SharePoint.WebPartPages.Communication
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

'Declaration
<ObsoleteAttribute("Use System.Web.UI.WebControls.WebParts.IWebPartRow instead")> _
Public Interface IRowProvider
'Usage
Dim instance As IRowProvider
[ObsoleteAttribute("Use System.Web.UI.WebControls.WebParts.IWebPartRow instead")]
public interface IRowProvider

Remarks

The IRowProvider interface should be implemented for Web Parts that need to pass a collection of data that can be characterized as a row, such as a row in a table. It can be used in scenarios where the consumer Web Part was designed with an understanding of the data schema being sent by the provider. Also, a Web Part that implements the IRowProvider interface has the ability to pass initialization arguments to the consumer part. The IRowProvider interface can connect to more interfaces than any other connection interface. It can connect to ICellConsumer, IRowConsumer, IFilterConsumer, and IParametersInConsumer interfaces. Also, server-side implementations of the IRowProvider interface can be connected to Web Parts on a different page by using a Microsoft SharePoint Foundation–compatible Web Page editor, such as Microsoft SharePoint Designer. Connecting the IRowProvider interface to an IRowConsumer interface is a direct connection so no transformer dialog box will be displayed. When connecting the IRowProvider interface to the ICellConsumer, IFilterConsumer, or IParametersInConsumer interface, a transformer dialog box will be displayed that allows an end user to map the row values between the parts. In this case, the consuming part doesn't need to be designed with knowledge of the data schema being sent by the provider Web Part because the end user is performing the mapping. However, this does require a user with a deeper understanding of the functionality of the Web Parts.

Examples

The following code example shows a simple Web Part that implements the IRowProvider interface only for connections that run on the server. It can be connected to one or more Web Parts which implement the ICellConsumer, IRowConsumer, IFilterConsumer, or IParametersInConsumer interface on the server. This example displays a list of products with a selection button for each row. When the button is clicked, this Web Part will pass the row to all other Web Parts which are connected to it.

There are 9 steps specific to making this a connectable Web Part. These steps are numbered and commented in the code example.

For an overview of the steps of creating a connectable Web Part, see 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;
            }
        }
    }
}

See Also

Reference

IRowProvider Members

Microsoft.SharePoint.WebPartPages.Communication Namespace