Export (0) Print
Expand All

Displaying Database Information Using a Table Web Server Control

Visual Studio .NET 2003

It's possible to display database information using the Table Web server control.

Tip   For many applications, it is easier to display data-bound information in a table by using the Repeater, DataList, or DataGrid Web server controls.

In the most common scenario for displaying data in a Table Web server control, each row of the table represents a data row from the data source, and each cell of the table displays a single field from a row. Unlike the list controls (Repeater, DataList, and so on), the Table control does not have a mechanism for automatically iterating through the rows in a data source. Therefore, you must do this yourself.

For general details about data binding in Web server controls, see Web Forms Data Binding.

To display database information in a Table Web server control

  1. Add a dataset to the page. For information on how to do this, see Walkthrough: Creating a Distributed Application, Walkthrough: Simple Data Access in a Windows Form, or Creating Typed Datasets with the Component Designer. For a discussion of options in creating datasets from database tables, see Introduction to ADO.NET Connection Design Tools, Introduction to Data Adapters, and Introduction to Datasets.
  2. Add a row to the table for each row in the dataset, and then set the contents of each TableCell object in the row to the data to be displayed. Most often you set the cell's Text property to an expression that extracts data from a data source. It's also possible to include controls in a TableCell control and bind the controls to a source of data. For details about using controls to display information, see Adding Rows and Cells Dynamically to a Table Web Server Control.

    The following example illustrates one way to display data in a Table Web server control. The sample shows an event handler for a button. The method loops through the DefaultView object of a dataset, creating a TableRow control for each row in the dataset. The code then creates a TableCell control in each table row and sets its Text property to the "name" field of the current data row.

    ' Visual Basic
    Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
       Dim aDataView As DataView = authorData.Tables(0).DefaultView
       Dim aRow As DataRowView
       For Each aRow In aDataView
          Dim aTableRow As New TableRow()
          Dim aCell As New TableCell()
          aCell.Text = aRow.Item("last").ToString()
          aTableRow.Cells.Add(aCell)
          Table1.Rows.Add(aTableRow)
       Next
    End Sub
    
    // C#
    private void Button1_Click(object sender, System.EventArgs e)
    {
       DataView aDataView = authorData.Tables[0].DefaultView;
       foreach( DataRowView aRow in aDataView)
       {
          TableRow aTableRow = new TableRow();
          TableCell aCell = new TableCell();
          aCell.Text = aRow["last"].ToString();
          aTableRow.Cells.Add(aCell);
          Table1.Rows.Add(aTableRow);
       }
    }
    
    Note   Controls that you add dynamically to a Web Forms page do not automatically become part of the page's view state — neither the controls nor their values are saved when a page performs a round trip to the server. You are therefore responsible for saving the state of any dynamically-generated controls whose values you want to preserve. For details, see Introduction to Web Forms State Management.

See Also

Introduction to the Table Web Server Control | Adding Rows and Cells Dynamically to a Table Web Server Control | Web Forms Data Binding | Table Class

Show:
© 2014 Microsoft