Adding DataColumn to a Dataset and DataTable

Adding DataColumn to a Dataset and DataTable

   DataSet can be filled with data in multiple ways. One of them is to add data manually. The data can be added either to a new and empty DataSet or to an existing dataset, which already contains some data.

  Now as all .Net languages are strongly typed, they need a clear and unambiguous specification of Data types to be matched with a Row of data. So, while adding data manually to data set, the DataSet and the DataTable should to be prepared with the right column types for the dataset to accept the right type of data. The columns in a dataset is some what synonymous to the Columns in a Database. The class DataColumn is used for adding the Column Details or Type details on a DataSet's DataTable. A DataSet contains a collection of DataTables which are used to hold the Rows of Data. This DataTable contains a collection of DataColumn, which represent the meta data of the table. 

   The DataTable also contains another collection called DataRow. Each DataRow represents a row which is similar to the Database tuple or row or record. But this may not contain all the rows in a table because, what a DataTable will contain is only the rows that are populated using the SELECT Query. And the SELECT Query may contain either all the fields in a table or few fields or a set of fields which are a mixture of data from two or more tables. Although this is the case, the DataRow can still be considered somewhat equal to a Database Row.

   This article actually tries to explain how to add different DataColumn to the DataTable. The following descriptions contain code snippets which can be used for creating the DataColumn for some basic .Net types.

Creating an Integer Column:

    The integer is a basic data type. This is represented in data bases with various names. Some databases have it as number, some as integer etc., An integer is considered to be of 4 bytes commonly. But it differs depending on various implementations. .Net considers it to be of 4 bytes. 

    The following code can create a DataColumn with support for adding integer data rows.

        DataColumn dc = new DataColumn("IntegerExample",System.Type.GetType("System.Int32"));
        DataSetVariable.Tables["DataTableName"].Columns.Add(dc);

    This column may also be converted into an auto-increment column by adding the following line while creating it.

dc.AutoIncrement = true;
dc.AutoIncrementSeed = 1;
dc.AutoIncrementStep = 1;
dc.ReadOnly = true;

Creating a String DataColumn:

   String is the most common data type which is supported extensively in .Net. This is a stark contrast when compared with the legacy C++ compilers. A string in .Net is considered as a Unicode string by default and is manipulated using System.String class.

    The following code can create a DataColumn with support for adding string data rows.

        DataColumn dc = new DataColumn("StringExample",System.Type.GetType("System.String"));
        DataSetVariable.Tables["DataTableName"].Columns.Add(dc);

Creating a Boolean Column:

   The code snippet which may be used for creating a Boolean column is

        DataColumn dc = new DataColumn("BooleanExample",System.Type.GetType("System.Boolean"));
        DataSetVariable.Tables["DataTableName"].Columns.Add(dc);

Creating a Date Column:

   The DateTime is a data type which contains both the date as well as the time. It is manipulated using the System.DateTime class in .Net.

        DataColumn dc = new DataColumn("DateExample",System.Type.GetType("System.DateTime"));
        DataSetVariable.Tables["DataTableName"].Columns.Add(dc);

   The above samples are for some of the frequently used data types in creating columns for the DataTable. After adding this Meta data, the data can be added to the table as DataRow, which will contain the data for the above columns. 

   To find out more about the data types in .net read the Codersource.net article on .Net Data Types or MSDN article on data types.

Close    To Top
  • Prev Article-Programming:
  • Next Article-Programming:
  • Now: Tutorial for Web and Software Design > Programming > dotnet > Programming Content
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Geek Tutorial
     

    Blogging Tutorial

      RSS Tutorial
      Podcasting Tutorial
    Graphic Design Tutorial
      Coreldraw Tutorial
      Illustrator Tutorial
      3D Tutorials
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial/ Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial/ Articles
     

    XML Style

      AJAX Tutorial
      XML Mobile
    Flash Tutorial/ Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial/ Articles
      Linux Tutorial
      Symbian Tutorial
      MacOS Tutorial
    Personal Tech
      Hardware Tutorial
      Software Tutorial
      Online Auction