Display data in ASP.NET Table Control

While for many applications, it is easier to display data-bound information using the Repeater, DataList, or DataGrid, it is possible to display database information Using the Table Web server control.

In the most common scenario for displaying data in a Table 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, etc. ), 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.

  • Set the contents of a TableCell control 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 is 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 Control.

The following example illustrates one way to display data in a Table control. The sample shows an event-handling method 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 “Id” field of the current data row.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DynamicTextBox.aspx.cs" Inherits="DynamicTextBox" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
   <title>Untitled Page</title>
</head>
<body>
   <form id="form1" runat="server">
   <div>
       <asp:Table ID="Table1" runat="server" CellPadding="2" CellSpacing="2">
       </asp:Table>
       <asp:Button ID="btnFill" runat="server" Text="Fill" OnClick="btnFill_Click" />
   </div>
   </form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class DynamicTextBox : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {


   }
   public DataTable GetCustomMadeDataTable()
   {

       //Create a new DataTable object

       System.Data.DataTable objDataTable = new System.Data.DataTable();

       //Create three columns with string as their type

       objDataTable.Columns.Add("Id", typeof(string));
       objDataTable.Columns.Add("Column1", typeof(string));
       objDataTable.Columns.Add("Column2", typeof(string));
       objDataTable.Columns.Add("Column3", typeof(string));


       //Adding some data in the rows of this DataTable

       DataRow dr;
       for (int i = 0; i <= 10; i++)
       {
           dr = objDataTable.NewRow();
           dr[0] = "Id" + i.ToString();
           dr[1] = "Item" + i.ToString();
           dr[2] = "Product" + i.ToString();
           dr[3] = "Description" + i.ToString();
           objDataTable.Rows.Add(dr);
       }
       DataColumn[] dcPk = new DataColumn[1];
       dcPk[0] = objDataTable.Columns["Id"];


       objDataTable.PrimaryKey = dcPk;
       Session["dtTemp"] = objDataTable;
       return objDataTable;
   }
   protected void btnFill_Click(object sender, EventArgs e)
   {
       DataTable dt = GetCustomMadeDataTable() as DataTable;
       DataView dv = dt.DefaultView;
       // Create a row with one column for each row in the DataView 
       foreach (DataRowView dataRow in dv)
       {
           TableRow tRow = new TableRow();
           TableCell tCell = new TableCell();
           TableCell tCell2 = new TableCell();
           TableCell tCell3 = new TableCell();
           tCell.Text = dataRow.Row.ItemArray[0].ToString();
           tCell2.Text = dataRow.Row.ItemArray[1].ToString();
           tCell3.Text = dataRow.Row.ItemArray[2].ToString();
           tRow.Cells.Add(tCell);
           tRow.Cells.Add(tCell2);
           tRow.Cells.Add(tCell3);
           Table1.Rows.Add(tRow);
       }
   }
}

Please do not post any spam link in the comment box😊

Post a Comment (0)
Previous Post Next Post