Compartir a través de


Enlazar datos SQL a un control DataGrid

El versátil control de servidor DataGrid muestra datos tabulares y permite seleccionar, ordenar y editar los datos. De manera predeterminada, DataGrid genera una columna enlazada para cada campo del origen de datos (AutoGenerateColumns=true). Cada campo de datos aparece en una columna diferente en el orden en que está almacenado en la base de datos. En el siguiente ejemplo se muestra una lista con los nombres, direcciones, números de teléfono y otros datos de los autores. Para ver la ejecución de un ejemplo similar, hay que ejecutar el ejemplo DataGrid1.aspx del Tutorial de ASP.NET.

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<script language="VB" runat="server">
   Sub Page_Load(Src As Object, e As EventArgs) 
      Dim myConnection As SqlConnection
      Dim myCommand As SqlDataAdapter
      ' Create a connection to the "pubs" SQL database located on the 
      ' local computer.
      myConnection = New SqlConnection("server=localhost;" _ 
         & "database=pubs;Trusted_Connection=Yes")
      ' Connect to the SQL database using a SQL SELECT query to get all 
      ' the data from the "Authors" table.
      myCommand = new SqlDataAdapter("SELECT * FROM Authors", _ 
         myConnection)
      ' Create and fill a DataSet.
      Dim ds As DataSet = new DataSet()
      myCommand.Fill(ds)
      ' Bind MyDataGrid to the DataSet. MyDataGrid is the 
      ' ID for the DataGrid control in the HTML section.
      MyDataGrid.DataSource = ds 
      MyDataGrid.DataBind()
   End Sub
</script>

<body>
   <h3><font face="Verdana">
      Simple Select to a DataGrid Control.
   </font></h3>
   <ASP:DataGrid id="MyDataGrid" runat="server"
      Width="700"
      BackColor="#ccccff" 
      BorderColor="black"
      ShowFooter="false" 
      CellPadding=3 
      CellSpacing="0"
      Font-Name="Verdana"
      Font-Size="8pt"
      HeaderStyle-BackColor="#aaaadd"
      EnableViewState="false"
   />
</body>
</html>

[C#]
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<script language="C#" runat="server">
   protected void Page_Load(Object Src, EventArgs E) 
   {
      // Create a connection to the "pubs" SQL database located on the 
      // local computer.
      SqlConnection myConnection = new SqlConnection("server=localhost;" +
         "database=pubs;Trusted_Connection=Yes");
      // Connect to the SQL database using a SQL SELECT query to get all 
      // the data from the "Authors" table.
      SqlDataAdapter myCommand = new SqlDataAdapter("SELECT " +
         " * FROM Authors", myConnection);
      // Create and fill a DataSet.
      DataSet ds = new DataSet();
      myCommand.Fill(ds);
      // Bind MyDataGrid to the DataSet. MyDataGrid is the ID for the 
      // DataGrid control in the HTML section.
      DataView source = new DataView(ds.Tables[0]);
      MyDataGrid.DataSource = source ;
      MyDataGrid.DataBind();
   }
</script>

<body>
   <%-- Display the DataGrid information in the  body of the page. --%>
   <h3><font face="Verdana">Simple SELECT to a DataGrid Control
   </font></h3>
   <%-- Display the data in a DataGrid. --%>
   <ASP:DataGrid id="MyDataGrid" runat="server"
      Width="700"
      BackColor="#ccccff" 
      BorderColor="black"
      ShowFooter="false" 
      CellPadding=3 
      CellSpacing="0"
      Font-Name="Verdana"
      Font-Size="8pt"
      HeaderStyle-BackColor="#aaaadd"
      EnableViewState="false"
   />
</body>
</html>

Vea también

Acceso a datos con ASP.NET | Acceso a datos con ADO.NET | System.Web.UI.WebControls (Espacio de nombres) | DataGrid (Control)