SPDataSource Constructor

Initializes a new instance of the SPDataSource class.

Namespace:  Microsoft.SharePoint.WebControls
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: No

Syntax

'Declaration
Public Sub New
'Usage

Dim instance As New SPDataSource()
public SPDataSource()

Remarks

This constructor creates a new instance of the SPDataSource class. You can bind this instance to any server control that derives from the DataBoundControl class. For more information, see the DataBind method.

Examples

The following example is a Web Part that initializes an SPDataSource control so that it is set to List mode and configured to query the Contacts list in the root Web site of the current site collection.

The code for the Web Part also initializes an GridView control. In the RenderContents method, this control is configured and then bound to the data source control.

Imports System
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports Microsoft.SharePoint.WebControls

Public Class ContactViewer
   Inherits WebPart

   Private datasource As SPDataSource
   Private grid As GridView

   Protected Overrides Sub CreateChildControls()
      MyBase.CreateChildControls()

      ' Instantiate the SPDataSourceControl.
      Me.datasource = New SPDataSource

      ' Initialize it.
      Me.datasource.UseInternalName = True
      Me.datasource.DataSourceMode = SPDataSourceMode.List
      Me.datasource.SelectCommand = "<View />"
      Me.datasource.SelectParameters.Add("WebId", TypeCode.String, "rootweb")
      Me.datasource.SelectParameters.Add("ListName", TypeCode.String, "Contacts")

      ' Add it to the Controls collection.
      Me.Controls.Add(Me.datasource)

      ' Instantiate the GridView control.
      Me.grid = New GridView()

      ' Add it to the Controls collection.
      Controls.Add(Me.grid)

   End Sub

   Protected Overrides Sub RenderContents(ByVal writer As System.Web.UI.HtmlTextWriter)

      ' Set up the field bindings
      Dim boundField As BoundField = New BoundField
      boundField.HeaderText = "Last Name"
      boundField.DataField = "Title"
      Me.grid.Columns.Add(boundField)

      boundField = New BoundField()
      boundField.HeaderText = "First Name"
      boundField.DataField = "FirstName"
      Me.grid.Columns.Add(boundField)

      ' Format the grid.
      Me.grid.AutoGenerateColumns = False
      Me.grid.CssClass = "ms-listviewtable"
      Me.grid.AlternatingRowStyle.CssClass = "ms-alternating"
      Me.grid.Width = New Unit(50, UnitType.Percentage)
      Me.grid.GridLines = GridLines.None
      Me.grid.HeaderStyle.Font.Bold = True
      Me.grid.HeaderStyle.HorizontalAlign = HorizontalAlign.Left

      ' Bind it to the data source.
      Me.grid.DataSource = Me.datasource
      Me.grid.DataBind()

      Me.grid.RenderControl(writer)

   End Sub

End Class
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint.WebControls;

namespace SampleWebParts
{
   public class ContactViewer : WebPart
   {
      private SPDataSource datasource;
      private GridView grid;

      protected override void CreateChildControls()
      {
         base.CreateChildControls();

         // Instantiate the SPDataSource control.
         this.datasource = new SPDataSource { 
            UseInternalName = true, 
            DataSourceMode = SPDataSourceMode.List,
            SelectCommand = "<View/>"
         };

         // Initialize it.
         this.datasource.SelectParameters.Add("WebId", TypeCode.String, "rootweb");
         this.datasource.SelectParameters.Add("ListName", TypeCode.String, "Contacts");

         // Add it to the controls collection.
         this.Controls.Add(this.datasource);

         // Instantiate and add the GridView control.
         this.grid = new GridView();
         this.Controls.Add(this.grid);
      }

      protected override void RenderContents(HtmlTextWriter writer)
      {

         // Set up the field bindings.
         BoundField boundField = new BoundField();
         boundField.HeaderText = "Last Name";
         boundField.DataField = "Title";
         this.grid.Columns.Add(boundField);

         boundField = new BoundField();
         boundField.HeaderText = "First Name";
         boundField.DataField = "FirstName";
         this.grid.Columns.Add(boundField);

         // Format the grid.
         this.grid.AutoGenerateColumns = false;
         this.grid.CssClass = "ms-listviewtable";
         this.grid.AlternatingRowStyle.CssClass = "ms-alternating";
         this.grid.Width = new Unit(50, UnitType.Percentage);
         this.grid.GridLines = GridLines.None;
         this.grid.HeaderStyle.Font.Bold = true;
         this.grid.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;

         // Bind to the data source.
         this.grid.DataSource = datasource;
         this.grid.DataBind();

         this.grid.RenderControl(writer);
      }
   }
}

See Also

Reference

SPDataSource Class

SPDataSource Members

SPDataSource Overload

Microsoft.SharePoint.WebControls Namespace