Export (0) Print
Expand All

Binding SQL Data to a DataGrid Control

.NET Framework 1.1

The versatile DataGrid server control displays tabular data and supports selecting, sorting, and editing the data. By default, DataGrid generates a bound column for each field in the data source (AutoGenerateColumns=true). Each data field is displayed in a separate column in the order it is stored in the database. The following example displays a list of authors' names, addresses, phone numbers, and other data. To see a similar example executed, run the DataGrid1.aspx sample in the ASP.NET QuickStart.

<%@ 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>

See Also

Accessing Data with ASP.NET | Accessing Data with ADO.NET | System.Web.UI.WebControls Namespace | DataGrid Control

Show:
© 2014 Microsoft