Home

Data Tables

 

Fundamentals

 

Introduction to Tables

Imagine you have a list of movie directors and you want to group their names into a list. Here is an example:

Rob Reiner, Jonathan Lynn, Bruce Beresford, Jonathan Demme, Adrian Lyne

This is a one-dimensional list like a simple array. While working on this list, you may decide to create a video collection and make the above items into a formal list. A typical movie provides such information as its length, its rating, the year it was released, etc. To create such a list, you would group items by categories. One category may contain the titles of the videos. Another category may contain the names of the directors, and so on.

To better organize a list, you may create each category, then enter the value of each category that corresponds to a particular video. Here is an example:

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

This type of list is called a table: A table is a two-dimensional list that contains one or different categories of items and each category is represented with a particular value. A category of values is called a column. Under each category, you may have a group of values that belong to the same entry. Such a group of values is called a row or a record. In the above table, the values "A Few Good Men", "Rob Reiner", "1992", "138 Minute", "VH", and "R" constitute one row or record.

Practical Learning Practical Learning: Introducing Tables

  1. Start Microsoft Visual C# and create a Console Application named VideoCollection1
  2. To create a new class, in the Solution Explorer, right-click VideoCollection1 -> Add -> Class...
  3. Set the Name to Video and press Enter
  4. Change the file as follows:
     
    using System;
    using System.Data;
    
    namespace VideoCollection1
    {
        public class Video
        {
            public DataSet dsVideoCollection;
    
            public Video()
            {
                dsVideoCollection = new DataSet("Videos");
            }
        }
    }

Creating a Table

To support the creation and management of a table, the .NET Framework provides the DataTable class that is defined in the System.Data namespace. There are various ways you can create a table. You can declare a variable of type DataTable. To initialize the variable, the DataTable class is equipped with three constructors. The default constructor allows you to create a table without giving more details, especially without formally naming it. Here is an example:

using System;
using System.Data;

public class VideoCollection
{
    public DataSet dsVideoCollection;

    public VideoCollection()
    {
        dsVideoCollection = new DataSet("Videos");

        DataTable tblDirectors = new DataTable();
    }
}

If you are planning to refer to the table from more than one method, you should declare it globally. Here is an example:

using System;
using System.Data;

public class VideoCollection
{
    public DataTable tblDirectors;
    public DataSet dsVideoCollection;

    public VideoCollection()
    {
        dsVideoCollection = new DataSet("Videos");

        tblDirectors = new DataTable();
    }
}

The name of the DataTable variable is required when creating a table. This name will be used by you and the compiler to identify the table. In some cases, you may want the table to hold an object name that you would want to use later as we will see with some methods. To provide a formal name to a table when creating it, you can use the second constructor of the DataTable class. Its syntax is:

public DataTable(string tableName);

This constructor expects as argument a string that would constitute the object name of the table. Here is an example:

using System;
using System.Data;

public class VideoCollection
{
    public DataTable tblDirectors;
    public DataTable tblVideoCategories;
    public DataSet dsVideoCollection;

    public VideoCollection()
    {
        dsVideoCollection = new DataSet("Videos");

        tblDirectors = new DataTable();
        tblVideoCategories = new DataTable("Categories");
    }
}

If you have already declared a DataTable variable using either of both constructors and decide to specify or change its name, you can assign a string to the DataTable.TableName property. Here is an example:

using System;
using System.Data;

public class VideoCollection
{
    public DataTable tblDirectors;
    public DataTable tblVideoCategories;
    public DataSet dsVideoCollection;

    public VideoCollection()
    {
        dsVideoCollection = new DataSet("Videos");

        tblDirectors = new DataTable();
        tblDirectors.TableName = "Directors";
        tblVideoCategories = new DataTable("Categories");
    }
}

The TableName name is primarily optional but it is used by various methods as we will see. If you don't provide it, for example if you declare a DataTable variable using the default constructor and don't specify its name through the DataTable.TableName property, the compiler would generate a default name for the table, such as Table1, Table2, and so on. In the same way, you can create as many tables as you judge necessary for your application and as we will see when moving on.

After creating a table, you can add it to a DataSet object.

 

Previous Copyright © 2006-2016, FunctionX, Inc. Next