.Net Controls: DataTable |
|
Overview |
A table is a two-dimensional object made of vertical series, called columns, and horizontal series, called rows. Based on this arrangement, a table is the most fundamental object of a list. Therefore, tables are highly used in databases. Although the word table is usually automatically associated with the concept of a database, which would imply that you need an external application to create a table, Microsoft .Net makes it easy and convenient to create a table, even if you are not creating a traditional database application. |
Table Creation |
To support the creation of tables, the System.Data namespace provided a class called DataTable. Therefore, in order to create a table, you can first declare a DataTable pointer. Here is an example: DataTable dtPersons = new DataTable; In order to provide its functionality, a table is divided in vertical sections called columns. By definition, a table should have at least one column, otherwise it would mean nothing. The column of a table is created using the DataColumn class. To create a column, you can declare a DataColumn variable using one of its constructors. The default constructor allows you to create a column before providing its details. Here is an example: DataColumn dcCategory = new DataColumn; When a table comes up (or when it gets updated), the user must be able to identify each column. For this reason, a column displays a string on top. This string is also referred to as its name. Sometimes it is also called a caption. After using the default constructor, you can call the DataColumn.ColumnName property to specify the string that would display on top of the column; that is, its caption. Here is an example: dcCategory.ColumnName = "Full Name"; Instead of using the default constructor, you can call the DataColumn(String) constructor to provide the caption of the column when creating it. Here is an example: dcCategory = new DataColumn("Email Address"); Besides the caption of the column, you can specify the kind of information that would be displayed by the column. After creating a columns using one of the above constructors, you can call the DataColumn.DataType property to specify this information. Here is an example: dcCategory.DataType = System.Type.GetType("System.String"); The DataColumn(String, Type) constructor allows you to specify both the caption and the data type when creating the column. Here is an example: dcCategory = new DataColumn("Marital Status", System.Type.GetType("System.String")); Once a column has been created, you can add it to the table. To support this, the DataTable class is equipped with the Columns property, which itself is a collection. Here is an example that creates a table and its columns: |
private: System.Void button1_Click(System.Object sender, System.EventArgs e) { DataTable dtPersons = new DataTable; DataColumn dcCategory = new DataColumn; dcCategory.ColumnName = "Full Name"; dcCategory.DataType = System.Type.GetType("System.String"); dtPersons.Columns.Add(dcCategory); dcCategory = new DataColumn("Email Address"); dcCategory.DataType = System.Type.GetType("System.String"); dtPersons.Columns.Add(dcCategory); dcCategory = new DataColumn("Marital Status", System.Type.GetType("System.String")); dtPersons.Columns.Add(dcCategory); dsPersons.Tables.Add(dtPersons); }
After creating a table, you can use it as you see fit in an application. For example, you can display it in a grid control. To do that, first you can create a data set using the DataSet class. Then, simply specify the data set as the data source of the grid control. Here is an example: |
private: System.Void button1_Click(System.Object sender, System.EventArgs e) { DataSet dsPersons = new DataSet("Employees Records"); DataTable dtPersons = new DataTable; DataColumn dcCategory = new DataColumn; dcCategory.ColumnName = "Full Name"; dcCategory.DataType = System.Type.GetType("System.String"); dtPersons.Columns.Add(dcCategory); dcCategory = new DataColumn("Email Address"); dcCategory.DataType = System.Type.GetType("System.String"); dtPersons.Columns.Add(dcCategory); dcCategory = new DataColumn("Marital Status", System.Type.GetType("System.String")); dtPersons.Columns.Add(dcCategory); dsPersons.Tables.Add(dtPersons); this.dataGrid1.DataSource = dsPersons; this.dataGrid1.DataMember = "Table1"; } |
As mentioned already, a table is made of columns and and other horizontal divisions called rows. A row is also called a record. A row in a table is created using the DataRow class. Therefore, to create a row, you can first declare an instance of the DataRow class. Here is an example: DataRow drPersons; To make its information efficient, a record can specify a value for each column, or some columns, of the table. In order to provide this value, you must first specify the column whose value you want to provide. Therefore, the DataRow class is equipped with an Item property which itself overloads the [] operation. Between the square brackets, specify caption of the column, then assign the desired value. Obviously the value should be conform to the type specified when creating the column. After specifying the values of a record, call the DataTable variable, which is equipped with a property called Rows. The DataTable.Rows is a collection of rows and is a DataRowCollection object. The DataRowCollection class is equipped with the Add() method that allows you to add a row to the collection. Here is an example of a table, its columns and rows: |
private: System.Void button1_Click(System.Object sender, System.EventArgs e) { DataSet dsPersons = new DataSet("Employees Records"); DataTable dtPersons = new DataTable; DataColumn dcCategory = new DataColumn; dcCategory.ColumnName = "Full Name"; dcCategory.DataType = System.Type.GetType("System.String"); dtPersons.Columns.Add(dcCategory); dcCategory = new DataColumn("Email Address"); dcCategory.DataType = System.Type.GetType("System.String"); dtPersons.Columns.Add(dcCategory); dcCategory = new DataColumn("Marital Status", System.Type.GetType("System.String")); dtPersons.Columns.Add(dcCategory); dsPersons.Tables.Add(dtPersons); DataRow drPersons; drPersons = dtPersons.NewRow(); drPersons.Item["Full Name"] = "Anselme Bongos"; drPersons.Item["Email Address"] = "bongosa@netscape.net"; drPersons.Item["Marital Status"] = "Married"; dtPersons.Rows.Add(drPersons); drPersons = dtPersons.NewRow(); drPersons.Item["Full Name"] = "Sylvie Ngouba"; drPersons.Item["Email Address"] = "ngoubas@hotmail.com"; drPersons.Item["Marital Status"] = "Divorced"; dtPersons.Rows.Add(drPersons); this.dataGrid1.DataSource = dsPersons; this.dataGrid1.DataMember = "Table1"; } |
|
||
Home | Copyright © 2004-2010 FunctionX, Inc. | |
|