Export (0) Print
Expand All

SPDataSource Constructor

Windows SharePoint Services 3

Initializes a new instance of the SPDataSource class.

Namespace:  Microsoft.SharePoint.WebControls
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

public SPDataSource()

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.

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.

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);
      }
   }
}

Community Additions

ADD
Show:
© 2014 Microsoft