BaseDataList.DataSource Property
Gets or sets the source containing a list of values used to populate the items within the control.
[Visual Basic] Public Overridable Property DataSource As Object [C#] public virtual object DataSource {get; set;} [C++] public: __property virtual Object* get_DataSource(); public: __property virtual void set_DataSource(Object*); [JScript] public function get DataSource() : Object; public function set DataSource(Object);
Property Value
An System.Collections.IEnumerable implemented object that contains a collection of values used to supply data to this control. The default value is a null reference (Nothing in Visual Basic).
Remarks
Use the DataSource property to specify the source of values to bind to a data listing control. The data source must be an object that implements the System.Collections.IEnumerable interface (such as System.Data.DataView, System.Collections.ArrayList, and System.Collections.Hashtable) to bind to a control derived from the BaseDataList class.
Example
[Visual Basic] <%@ Page Language="VB" AutoEventWireup="True" %> <%@ Import Namespace="System.Data" %> <html> <script language="VB" runat="server"> Function CreateDataSource() As ICollection Dim dt As New DataTable() Dim dr As DataRow dt.Columns.Add(New DataColumn("IntegerValue", GetType(Int32))) dt.Columns.Add(New DataColumn("StringValue", GetType(String))) dt.Columns.Add(New DataColumn("CurrencyValue", GetType(Double))) Dim i As Integer For i = 0 To 8 dr = dt.NewRow() dr(0) = i dr(1) = "Item " + i.ToString() dr(2) = 1.23 *(i + 1) dt.Rows.Add(dr) Next i Dim dv As New DataView(dt) Return dv End Function 'CreateDataSource Sub Page_Load(sender As Object, e As EventArgs) If Not IsPostBack Then ' Load this data only once. ItemsGrid.DataSource = CreateDataSource() ItemsGrid.DataBind() End If End Sub 'Page_Load </script> <body> <form runat=server> <h3>DataGrid Example</h3> <b>Product List</b> <asp:DataGrid id="ItemsGrid" BorderColor="black" BorderWidth="1" CellPadding="3" AutoGenerateColumns="true" runat="server"> <HeaderStyle BackColor="#00aaaa"> </HeaderStyle> </asp:DataGrid> </form> </body> </html> [C#] <%@ Page Language="C#" AutoEventWireup="True" %> <%@ Import Namespace="System.Data" %> <html> <script language="C#" runat="server"> ICollection CreateDataSource() { DataTable dt = new DataTable(); DataRow dr; dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32))); dt.Columns.Add(new DataColumn("StringValue", typeof(string))); dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double))); for (int i = 0; i < 9; i++) { dr = dt.NewRow(); dr[0] = i; dr[1] = "Item " + i.ToString(); dr[2] = 1.23 * (i + 1); dt.Rows.Add(dr); } DataView dv = new DataView(dt); return dv; } void Page_Load(Object sender, EventArgs e) { if (!IsPostBack) { // Load this data only once. ItemsGrid.DataSource= CreateDataSource(); ItemsGrid.DataBind(); } } </script> <body> <form runat=server> <h3>DataGrid Example</h3> <b>Product List</b> <asp:DataGrid id="ItemsGrid" BorderColor="black" BorderWidth="1" CellPadding="3" AutoGenerateColumns="true" runat="server"> <HeaderStyle BackColor="#00aaaa"> </HeaderStyle> </asp:DataGrid> </form> </body> </html>
[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family
See Also
BaseDataList Class | BaseDataList Members | System.Web.UI.WebControls Namespace | System.Collections.IEnumerable | System.Data.DataView | System.Collections.ArrayList | System.Collections.Hashtable | System.Data.DataSet