Questo articolo è stato tradotto automaticamente. Per visualizzare l'articolo in inglese, selezionare la casella di controllo Inglese. È possibile anche visualizzare il testo inglese in una finestra popup posizionando il puntatore del mouse sopra il testo.

Metodo GridView.DataBind ()


Data di pubblicazione: ottobre 2016

Associa l'origine dati al controllo GridView. Questo metodo non può essere ereditato.

Spazio dei nomi:   System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

public override sealed void DataBind()

Use the M:System.Web.UI.WebControls.GridView.DataBind method to bind data from a data source to the T:System.Web.UI.WebControls.GridView control. This method resolves all data-binding expressions in the active template of the control.

The M:System.Web.UI.WebControls.GridView.DataBind method is called automatically if the P:System.Web.UI.WebControls.DataBoundControl.DataSourceID property of the T:System.Web.UI.WebControls.GridView control refers to a valid data source control.

Instead of manually calling the M:System.Web.UI.WebControls.GridView.DataBind method, you can use model binding with your M:System.Web.UI.WebControls.GridView.DataBind control by setting the P:System.Web.UI.WebControls.DataBoundControl.SelectMethod property to the name of a method that returns data for the T:System.Web.UI.WebControls.GridView. The T:System.Web.UI.WebControls.GridView is then automatically populated with the data that is returned from the select method. Model binding can simplify your code for working with data. For more information, see Model Binding and Web Forms

The following example demonstrates how to use the M:System.Web.UI.WebControls.GridView.DataBind method to bind a data source to a T:System.Web.UI.WebControls.GridView control.

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<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.                      

      // 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;
        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();

      // Connect to the database and run the query.
      SqlConnection connection = new SqlConnection(connectionString);        
      SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection);

      // Fill the DataSet.

    catch(Exception ex)

      // The connection failed. Display an error message.
      Message.Text = "Unable to connect to the database.";


    return ds;



<html xmlns="" >
  <head runat="server">
    <title>GridView DataBind Example</title>
    <form id="form1" runat="server">

      <h3>GridView DataBind Example</h3>

      <asp:label id="Message"


      <asp:gridview id="AuthorsGridView" 


.NET Framework
Disponibile da 2.0
Torna all'inizio