BaseDataBoundControl.DataSource 속성

정의

데이터 바인딩된 컨트롤에서 데이터 항목의 목록을 검색할 소스 개체를 가져오거나 설정합니다.

[System.ComponentModel.Bindable(true)]
[System.Web.UI.Themeable(false)]
public virtual object DataSource { get; set; }

속성 값

데이터 바인딩된 컨트롤에서 데이터를 검색할 데이터 소스를 나타내는 개체입니다. 기본값은 null입니다.

특성

예제

다음 코드 예제에서는 방법을 DataSource 데이터 바인딩된 컨트롤의 속성을 사용 합니다. 이 예제는 GridView 컨트롤이 바인딩되는 DataSet 개체입니다. 후 합니다 DataSource 속성을 설정 합니다 DataBind 메서드는 명시적으로 호출 합니다.


<%@ Page language="C#" %>
<%@ import namespace="System.Data" %>
<%@ import namespace="System.Data.SqlClient" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  void Page_Load(Object sender, EventArgs e)
  {
    
    // This example uses Microsoft SQL Server and connects
    // to the Northwind sample database. The data source needs
    // to be bound to the GridView control only when the 
    // page is first loaded. Thereafter, the values are
    // stored in view state.                      
    if(!IsPostBack)
    {
        
      // Declare the query string.
      String queryString = 
        "Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]";
        
      // Run the query and bind the resulting DataSet
      // to the GridView control.
      DataSet ds = GetData(queryString);
      if (ds.Tables.Count > 0)
      {
        AuthorsGridView.DataSource = ds;
        AuthorsGridView.DataBind();
      }
      else
      {
        Message.Text = "Unable to connect to the database.";
      }
            
    }     
    
  }
    
  DataSet GetData(String queryString)
  {

    // Retrieve the connection string stored in the Web.config file.
    String connectionString = ConfigurationManager.ConnectionStrings["NorthWindConnectionString"].ConnectionString;      
 
    DataSet ds = new DataSet();
        
    try
    {
      // Connect to the database and run the query.
      SqlConnection connection = new SqlConnection(connectionString);        
      SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection);
        
      // Fill the DataSet.
      adapter.Fill(ds);
            
    }
    catch(Exception ex)
    {
        
      // The connection failed. Display an error message.
      Message.Text = "Unable to connect to the database.";
        
    }
        
    return ds;
        
  }
    
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>GridView DataBind Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>GridView DataBind Example</h3>
            
      <asp:label id="Message"
        forecolor="Red"
        runat="server"/>
               
      <br/>    

      <asp:gridview id="AuthorsGridView" 
        autogeneratecolumns="true" 
        runat="server">
      </asp:gridview>
                        
    </form>
  </body>
</html>

설명

설정한 경우는 DataSource 속성인은 ValidateDataSource 메서드가 호출 됩니다. 또한 데이터 바인딩된 컨트롤을 이미 초기화 된 경우에 OnDataPropertyChanged 메서드를 호출을 설정 하는 RequiresDataBinding 속성을 true입니다.

이 속성은 테마 또는 스타일시트 테마에 의해 설정될 수 없습니다. 자세한 내용은 ThemeableAttribute 하 고 ASP.NET 테마 및 스킨합니다.

적용 대상

제품 버전
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

추가 정보