Main Menu
Knowledge Base
Product Registration
Log an Incident
Request a Feature
Search Incidents/Bug Reports


Search KB

Please note: In an effort to better serve you, we are in the process of restructuring DevCenter. In the process, we have moved many items that you may be used to finding in DevCenter over to the Main Site. If you are having trouble locating something, please try looking at the following places:

Knowledge Base Article: KB02904

HOWTO:Bind to a DataTable


The information in this article applies to:
UltraWebGrid (v1.0.6007)
  Article Created: 
11/12/2002

Last Updated:
11/26/2002

Article Type
How To
  
Page Options
Average Rating:
2 out of 10

Rate this page
Print this page
E-mail this page
Add to Favorites

Summary

Background

The UltraWebGrid can use any DataSource that implements the IList, ITypedList or IBindList Interface as well as the DataTable and DataSet.
The DataTable can contain the results from a single DataBase Query and consists of rows and columns (This is sometimes referred to as Flat data as opposed to Hierarchical data which consists of more than one table and relations).

Issues

Many times the UltraWebGrid needs to display only the data from a single DataBase Query or only needs to display rows and columns.

Solution

The DataTable provides an ideal container for Row/Column data.

Additional Information

Sample Project

This project is very simple. A DataTable is created and bound to the UltraWebGrid.
When you press the "Bind DataTable to UltraWebGrid" button you will see something like this:

Code Discussion

All of the code relevant to this project is contained in the ButtonClick event for the "Bind DataTable to UltraWebGrid" button. It consists of the code required to create a simple DataTable and bind it to the UltraWebGrid.


Declare a New DataTable with the name "Customers":

' declare a DataTable to contain the program generated data
Dim dataTable As New System.Data.DataTable("Customers")

Create a New Column named "CustomerID" of type Int32 and add it to the DataTable:

' create and add a CustomerID column
Dim colWork As New DataColumn("CustomerID", GetType(Int32))
dataTable.Columns.Add(colWork)

Create an array of DataColumns to contain the columns of the primary key. Add the "CustomerID" column to the array and bind the array to the DataTable PrimaryKey property:

' add CustomerID column to key array and bind to DataTable
Dim Keys(0) As DataColumn
Keys(0) = colWork
dataTable.PrimaryKey = Keys

Create a New Column named "CustomerName" of type String with a MaxLength of 50 characters and add it to the DataTable:

' create and add a CustomerName column
colWork = New DataColumn("CustomerName", GetType(String))
colWork.MaxLength = 50
dataTable.Columns.Add(colWork)

Create a New Column named "LastOrderDate" of type Date and add it to the DataTable:

' create and add a LastOrderDate column
colWork = New DataColumn("LastOrderDate", GetType(Date))
dataTable.Columns.Add(colWork)

Create a new Row, populate the column values and add it to the DataTable:

' add a row
Dim row As DataRow = dataTable.NewRow()
row("CustomerID") = 1
row("CustomerName") = "John's Widgets"
row("LastOrderDate") = Now
dataTable.Rows.Add(row)

Create another new Row, populate the column values and add it to the DataTable:

' add another row
row = dataTable.NewRow()
row("CustomerID") = 2
row("CustomerName") = "Fred's Thingamagigs"
row("LastOrderDate") = Now.AddDays(-101)
dataTable.Rows.Add(row)

Bind the DataTable to the DataSource Property of the UltraWebGrid:

' bind DataTable to UltraWebGrid
UltraWebGrid1.DataSource = dataTable
UltraWebGrid1.DataBind()

Review
This project is very straightforward and demonstrates the ease with which a DataTable can be created, populated and bound to the UltraWebGrid with very few lines of code.

Samples

webgrid_datatable.zip
 Binding a WebGrid to a DataTable



How would you rate the quality of this content?
Poor -----------------------------------------> Outstanding

Tell us why you rated the content this way. (optional)