This project illustrates the program generation of a DataTable (Flat Data) and binding of this DataTable to the WinGrid™ control.
This exercise illustrates how to:
Programmatically Create a DataTable.
Add Columns to the DataTable.
Specify a Primary Key for the DataTable.
Add Rows to the DataTable.
Bind the DataTable to the UltraGrid.
The finished form should will something like this:
Create a new Windows Application project.
From the Toolbox Windows Forms tab, add an UltraButton to the form and set the Text property to "Create and Bind Flat Data". (See above screen shot for placement.)
From the Toolbox add an UltraGrid to the form.
Add a Sub Procedure to respond to the "Create and Bind Flat Data" button’s Click event and add the following code to the procedure:
In Visual Basic:
Private Sub UltraButton1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles UltraButton1.Click ' Declare a DataTable to contain the program generated data Dim dataTable As New DataTable("TableTest") ' Create and add a CustomerID column Dim colWork As New DataColumn("CustomerID", GetType(Int32)) dataTable.Columns.Add(colWork) ' Add CustomerID column to key array and bind to DataTable Dim Keys(0) As DataColumn Keys(0) = colWork dataTable.PrimaryKey = Keys ' Create and add a CustomerName column colWork = New DataColumn("CustomerName", GetType(String)) colWork.MaxLength = 50 dataTable.Columns.Add(colWork) ' Create and add a LastOrderDate column colWork = New DataColumn("LastOrderDate", GetType(Date)) dataTable.Columns.Add(colWork) ' Add a row Dim row As DataRow = dataTable.NewRow() row("CustomerID") = 1 row("CustomerName") = "John's Widgets" row("LastOrderDate") = Now dataTable.Rows.Add(row) ' 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 table to the grid Me.UltraGrid1.DataSource = dataTable End Sub
In C#:
private void ultraButton1_Click(object sender, EventArgs e) { // Declare a DataTable to contain the program generated data DataTable dataTable = new DataTable("TableTest"); // Create and add a CustomerID column DataColumn colWork = new DataColumn("CustomerID", System.Type.GetType("System.Int32")); dataTable.Columns.Add(colWork); // Add CustomerID column to key array and bind to DataTable DataColumn[] Keys = new DataColumn[1]; Keys[0] = colWork; dataTable.PrimaryKey = Keys; // Create and add a CustomerName column colWork = new DataColumn("CustomerName", System.Type.GetType("System.String")); colWork.MaxLength = 50; dataTable.Columns.Add(colWork); // Create and add a LastOrderDate column colWork = new DataColumn("LastOrderDate", System.Type.GetType("System.DateTime")); dataTable.Columns.Add(colWork); // Add a row DataRow row = dataTable.NewRow(); row["CustomerID"] = 1; row["CustomerName"] = "Johns Widgets"; row["LastOrderDate"] = System.DateTime.Now; dataTable.Rows.Add(row); // Add another row row = dataTable.NewRow(); row["CustomerID"] = 2; row["CustomerName"] = "Freds Thingamagigs"; row["LastOrderDate"] = System.DateTime.Now.AddDays(-101); dataTable.Rows.Add(row); // Bind the table to the grid this.ultraGrid1.DataSource = dataTable; }
Build and Run the Project. The form displays.
Press "Create and Bind Flat Data" and the Program Generated Flat Data displays in the UltraGrid.
This exercise shows how to create a DataTable with code and bind it to the WinGrid.