Override the DataBind Method on a TableControl Class


Instead of writing the ASP.NET data grid code from scratch, you can use an Iron Speed Designer-generated table control to populate your ASP.NET data grid, saving you a lot of programming.
- Pooja Daga, Technical Support Engineer, Iron Speed, Inc.

May 10, 2006

Introduction

In some situations it may be desirable to use an ASP.NET data grid in your application instead of a table control generated by Iron Speed Designer, for example:
  • Existing data grid. Your application may have a page that already has an ASP.NET data grid, and you may wish to use the existing ASP.NET data grid instead of adding an Iron Speed Designer-generated table control.
  • Simplicity. You might find it easier to work with an ASP.NET data grid instead of an Iron Speed Designer-generated table control.
  • Familiarity. You may already be familiar with the ASP.NET data grid and don’t have time to learn how to customize code for a new table control.
However, instead of writing the ASP.NET data grid code from scratch, you can use an Iron Speed Designer-generated table control to populate your ASP.NET data grid, saving you a lot of programming. Since the ASP.NET data grid is populated using the Iron Speed Designer-generated table control, features such as filtering, search and pagination are automatically implemented.

Procedure

Step 1: Create an application in Iron Speed Designer using a database table, such as the Orders table in Northwind.

Step 2: In the HTML layout page file for ShowOrdersTablePage, delete all content between the...

<!--Table View Area -->

and the...

<!-- Totals Area -->

...tags.

Step 3: Add the following content between the...

<!--Table View Area -->

and the...

<!-- Totals Area -->

...tags.

<tr>
    <td class="tableRowsEdge">
        <GEN:ITEMTEMPLATE Name="OrdersTableItem" ></GEN:ITEMTEMPLATE>
        <asp:datagrid id="myDataGrid" runat="server">
            <HeaderStyle BackColor = "#336699" ForeColor = "#ffffff" Font-Bold = "true" />
            <AlternatingItemStyle BackColor = "#eeeeee" />
        </asp:datagrid>
    </td>
</tr>

Step 4: Override the DataBind method in the TableControl class for ShowOrdersTablePage.aspx.

For .NET Framework 1.1, add the code in the TableControl class of ShowOrdersTablePage.aspx.cs located in:

...\<Application Folder>\Orders\ShowOrdersTablePage.aspx.cs

For .NET Framework 2.0, add the code in TableControl class of ShowOrdersTablePage.Controls.cs located in:

...\<Application Folder>\<App_Code>Orders\ShowOrdersTablePage.Controls.cs

C#:

public override void DataBind()
{
    base.DataBind();
    System.Collections.ArrayList myList;
    System.Data.DataTable myDataTable;
    myList= this.GetRecords();
    if(myList!=null)
    {
        OrdersRecord[] myRecordList ;
 
        //Convert to an array of type OrdersRecord.
        myRecordList =(OrdersRecord[])(myList.ToArray(typeof(OrdersRecord)));
 
        //Now convert this array to a datatable.
        myDataTable = OrdersTable.Instance.CreateDataTable(myRecordList);
        DataGrid myDataGridcontrol =
            (System.Web.UI.WebControls.DataGrid)this.Page.FindControlRecursively("myDataGrid");
 
        myDataGridcontrol.DataSource = myDataTable;
        myDataGridcontrol.DataBind();
    }
}

Visual Basic .NET:

Public Overrides Sub DataBind()
    MyBase.DataBind()
    Dim myList As System.Collections.ArrayList
    Dim myDataTable As System.Data.DataTable
    myList = Me.GetRecords()
 
    If Not (IsNothing(myList)) Then
        Dim myRecordsList As OrdersRecord()
 
        'Convert to an array of type OrdersRecord.
        myRecordsList = CType(myList.ToArray(GetType(OrdersRecord)), OrdersRecord())
 
        'Now convert this array to a datatable.
        myDataTable = OrdersTable.Instance.CreateDataTable(myRecordsList)
 
        'Now you can use the myDataTable and set it as the source of a data grid
        Dim myDataGridcontrol As DataGrid =
            CType(Me.Page.FindControlRecursively("myDataGrid"), System.Web.UI.WebControls.DataGrid)
 
        myDataGridcontrol.DataSource = myDataTable
        myDataGridcontrol.DataBind()
 
    End If
End Sub

Note that myDataGrid is the ID of the ASP .NET data grid.

About the Author

Pooja Daga
Technical Support Engineer, Iron Speed, Inc.

Pooja has experience developing Web applications using .NET technology. She started her career with PCS, a leading software services company headquartered in India and has been with Iron Speed since 2005.

Pooja holds a M.S. degree in Computer Application and a B.S. degree in Electrical engineering from Maharaja Sayajirao University in India.



  Privacy Statement