Home

The Columns of a Table

 

Introduction

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:

Video Title Director Year Length Format Rating
A Few Good Men Rob Reiner 1992 138 Minutes 1 R
The Distinguished Gentleman Jonathan Lynn   112 Minutes 2 R
The Lady Killers Joel Coen & Ethan Coen   104 Minutes 2 R
Fatal Attraction Adrian Lyne 1987 120 Minutes 1 R
Her Alibi Bruce Beresford 1989 94 Minutes 2 PG-13
The Manchurian Candidate Jonathan Demme 2004 129 Minutes 2 R

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.

Practical Learning Practical Learning: Introducing Columns

  1. Start Microsoft Visual C# and create a Console Application named VideoCollection2
  2. To create a new class, in the Class View, right-click VideoCollection2 -> Add -> Class...
  3. Set the Name to Video and press Enter
  4. Change the file as follows:
     
    using System;
    using System.Data;
    
    namespace VideoCollection2
    {
        public class Video
        {
            private DataSet dsVideoCollection;
    
            private DataTable tblActors;
            private DataTable tblVideos;
    
            public Video()
            {
                dsVideoCollection = new DataSet("VideoCollection");
    
                tblActors = new DataTable("Actors");
                dsVideoCollection.Tables.Add(tblActors);
    
                tblVideos = new DataTable("Videos");
                dsVideoCollection.Tables.Add(tblVideos);
            }
    
            public void ShowTables()
            {
                int i = 1;
    
                Console.WriteLine("Video Collection - Tables");
                foreach (DataTable tbl in dsVideoCollection.Tables)
                    Console.WriteLine("{0}. {1}", i++, tbl.TableName);
            }
        }
    }
  5. Access the Program.cs file and change it as follows:
     
    using System;
    
    namespace VideoCollection2
    {
        public class Program
        {
            static int Main(string[] args)
            {
                Video vdo = new Video();
                vdo.ShowTables();
    
                return 0;
            }
        }
    }
  6. To save the file, on the Standard toolbar, click the Save All button
  7. Accepts all defaults and click Save
  8. Execute the application to make sure it is working fine:
     
    Video Collection - Tables
    1. Actors
    2. Videos
    Press any key to continue . . .
  9. Close the DOS window

Creating a Column

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