Nigel Whitworth's Development Site

You are currently not logged in.

ASP.NET Datagrids

A new feature of ASP.NET is that creating a grid of data is a lot easier than it was in ASP. In the old system, you needed to write out the code to display the grid line by line. The new system just gets you to select the data then bind it to a datagrid and it'll do all the work itself. For our example, we have a database table of employees containing Employee_id, Employee_Surname and Employee_Forename. To get the data
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<html>

<script language="VB" runat="server">

    Sub Page_Load(Sender As Object, E As EventArgs)     ' Code is equivalent to Form_Load and Sub Main

        Dim DS As DataSet
        Dim MyConnection As SqlConnection
        Dim MyCommand As SqlDataAdapter

        MyConnection = New SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("PubsString"))
        MyCommand = New SqlDataAdapter("select Employee_ID, Employee_Surname, Employee_Forename from Employees", MyConnection)

        DS = New DataSet()
        MyCommand.Fill(DS, "Employees")

        DataGrid1.DataSource = DS.Tables("Employees").DefaultView
        DataGrid1.DataBind()
    End Sub

</script>

<body>

  <h3><font face="Verdana">Simple Stored Proc Select to a DataGrid Control</font></h3>

  <ASP:DataGrid id="DataGrid1" runat="server"
    Width="360"
    BackColor="#ccccff" 
    BorderColor="black"
    ShowFooter="false" 
    CellPadding=3 
    CellSpacing="0"
    Font-Name="Verdana"
    Font-Size="8pt"
    HeaderStyle-BackColor="#aaaadd"
    EnableViewState="false"
  />

</body>
</html>


Another way that we could display the data in a grid format is to use a dataRepeater, using the same scenario, we would code the page as :-
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<html>

<script language="VB" runat="server">

    Sub Page_Load(Sender As Object, E As EventArgs)     ' Code is equivalent to Form_Load and Sub Main

        Dim DS As DataSet
        Dim MyConnection As SqlConnection
        Dim MyCommand As SqlDataAdapter

        MyConnection = New SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("PubsString"))
        MyCommand = New SqlDataAdapter("select Employee_ID, Employee_Surname, Employee_Forename from Employees", MyConnection)

        DS = New DataSet()
        MyCommand.Fill(DS, "Employees")

        Repeater1.DataSource = DS.Tables("Employees").DefaultView
        Repeater1.DataBind()
    End Sub

</script>

<body topmargin="0" leftmargin="0" marginwidth="0" marginheight="0">


<ASP:Repeater id="Repeater1" runat="server">
    <HeaderTemplate>
        <table width="100%" style="font: 8pt verdana">
          <tr style="background-color:DFA894">
            <th>Employee ID</th>
            <th>Surname</th>
            <th>Forename</th>
          </tr>
      </HeaderTemplate>

      <ItemTemplate>
        <tr style="background-color:FFECD8">
          <td><%# DataBinder.Eval(Container.DataItem, "Employee_ID") %></td>
          <td><%# DataBinder.Eval(Container.DataItem, "Employee_Surname") %></td>
          <td><%# DataBinder.Eval(Container.DataItem, "Employee_Forename") %></td>
        </tr>
      </ItemTemplate>

      <FooterTemplate>
        </table>
      </FooterTemplate>

  </ASP:Repeater>

</Body>
</HTML>




If you intend to vote or make a comment, please enter the security code.

Make a comment

*Comments are the views of individuals, they may or may not be correct.
All comments are reviewed and accepted or rejected.
If you give an email address, you will be sent an email when someone makes a comment on this page.*

Name :
Email :
Comments :


Only name and Code is compulsory.
 
 
Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)