![]() |
The Columns of a Table |
A column is a technique of categorizing some values that belong to a table. Based on this, one category from a list of a video application can be made of video titles. Another category can contain the years the videos were released. Yet another category can contain a number used to identify the format of the video. Here is an example of a table:
A category of information of a table is called a column. The string on top of each column allows the user to identify what that column is used for. That string is called the column header. To support the columns of a table, the .NET Framework provides the DataColumn class.
To create a column, you can first declare a variable of type DataColumn. The DataColumn class is equipped with five constructors. The default constructor allows you to create a column without giving details. Here is an example: using System;
using System.Data;
public class VideoCollection
{
public VideoCollection()
{
DataColumn colCategoryID = new DataColumn();
}
}
If you are planning to reference the column from more than one method, you should declare it in a class. Here is an example: using System;
using System.Data;
public class VideoCollection
{
private DataColumn colCategoryID;
public VideoCollection()
{
colCategoryID = new DataColumn();
}
}
To distinguish them, each column must have a specific and unique name. The name of a column allows you and the compiler to identify a particular column. The name must follow the rules of variables in C#. To specify the object name of a column, when creating it, you can use the second constructor whose syntax is: public DataColumn(string name); This constructor expects as argument the name of the column. Here is an example: using System;
using System.Data;
public class VideoCollection
{
private DataColumn colCategoryID;
public VideoCollection()
{
colCategoryID = new DataColumn("CategoryID");
}
}
The name of a column is supported by the ColumnName property of the DataColumn class. This property is of type string. If you have already declared a DataColumn object, to specify or change its name, assign the desired string to the DataColumn.ColumnName property. Here is an example: using System;
using System.Data;
public class VideoCollection
{
private DataColumn colCategoryID;
private DataColumn colCategory;
public VideoCollection()
{
colCategoryID = new DataColumn("CategoryID");
colCategory = new DataColumn();
colCategory.ColumnName = "Category";
}
}
Based on these descriptions, the minimum information needed to create a column is a name. If you don't specify a name, a default name is assigned to the new column. |
|
|
||
| Previous | Copyright © 2006 FunctionX, Inc. | Next |
|
|
||