Home

Data Entry on a Table

 

Introduction

When you call the DataTable.NewRow() method, the record's status is DataRowState.Detached. After calling the DataTable.NewRow() method, you can specify the value that the column would carry. To do this, you must specify the table's column whose value you want to provide. You can locate a column based on an index as we mentioned already that the columns of a table are stored in the DataTable.Columns property which is based on the DataColumnCollection class. An example would be rowVideo["Title"], which specifies the column named Title. After specifying the column, assign it the desired but appropriate value based on the DataColumn.DataType value you would have provided. Here are examples of assigning values to the columns of a table:

using System;
using System.Data;

namespace VideoCollection
{
    public static  class Program
    {
        . . . No Change

        static void CreateRecord()
        {
            DataRow rowVideo = tblVideos.NewRow();

            rowVideo[0] = "GT-682";
            rowVideo[1] = "A Few Good Men";
            rowVideo[2] = "Rob Reiner";
            rowVideo[3] = "138 Minutes";
            rowVideo[4] = 1992;
            rowVideo[5] = "R";
        }

        static int Main(string[] args)
        {
            return 0;
        }
    }
}

Each column can also be identified by its index in the table. 

Adding a Record to a Table

After specifying the value(s) of the column(s), you must add it (them) to the table. To do this, you must call the Add() method of the DataRowCollection class. This method is overloaded with two versions. One of the versions uses the following syntax:

public void Add(DataRow row);

This method expects the name of the record as argument, which would be the value returned by a previous call to the DataTable.NewRow() method. Here is an example:

using System;
using System.Data;

namespace VideoCollection
{
    public static  class Program
    {
        . . . No Change

        static void CreateRecord()
        {
            DataRow rowVideo = tblVideos.NewRow();

            rowVideo[0] = "GT-682";
            rowVideo[1] = "A Few Good Men";
            rowVideo[2] = "Rob Reiner";
            rowVideo[3] = "138 Minutes";
            rowVideo[4] = 1992;
            rowVideo[5] = "R";

            tblVideos.Rows.Add(rowVideo);
        }

        static int Main(string[] args)
        {
            return 0;
        }
    }
}

When the record has been added to the table, the record has a status of DataRowState.Added.

Adding an Array of Records

The above version of the DataRowCollection.Add() method means that you must identify each column before assigning a value to it. If you already know the sequence of columns and don't need to explicitly identify them, you can store all values in an array and simply add the array as a complete record. To support this, the DataRowCollection class provide another version of the .Add() method whose syntax is:

public virtual DataRow Add(object[] values);

Here is an example:

using System;
using System.Data;

namespace VideoCollection
{
    public static  class Program
    {
        . . . No Change

        static void CreateRecord()
        {
            object[] arrRecord = { "MM-258", "Fatal Attraction", "Adrian Lyne", 1987,
			                       "120 Minutes", "R" };

            tblVideos.Rows.Add(arrRecord);
        }

        static int Main(string[] args)
        {
            return 0;
        }
    }
}

There is an alternative to this second version of the DataRowCollection.Add() method. As opposed to passing an array of values to the Add() method, you can first define an array, assign that array to a DataRow variable, then pass that DataRow object to the Add() method. To support this technique, the DataRow class is equipped with an ItemArray property that expects an array. Here is an example

using System;
using System.Data;

namespace VideoCollection
{
    public static  class Program
    {
        . . . No Change

        static void CreateRecord()
        {
            object[] arrVideo = { "FD-205", "Her Alibi", "Bruce Beresford",
                                     "94 Minute", 1989, "PG-13" };

            DataRow rowVideo = tblVideos.NewRow();
            rowVideo.ItemArray = arrVideo;

            tblVideos.Rows.Add(rowVideo);
        }

        static int Main(string[] args)
        {
            return 0;
        }
    }
}

After creating the records of a table, if a record contains invalid values, the DataRow.HasErrors property can help you identify them.

The Number of Records of a Table

After you have created a table and its columns but before adding any row, the number of the table's record is set to 0. Every time you add a new record, the number of records is incremented by 1. To get the number of records that a table contains, access the Count property of its DataRowCollection. The Count property is inherited from the InternalDataCollectionBase class, which is the parent of many collection classes.

 

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