DataTable, DataColumn and DataRow in ADO.NET

  • A K
  • Updated date Dec 27, 2011

Introduction

DataTable represents a single table. I have described about DataTable in my previous article. DataColumn represent schema of DataTable. We can add schema ( Column) of DataTable by DataColumn objects.

Let's work with DataColumn objects to add column in DataTable. Take a Window Form Application > take a dataGridView control and write the following code on form load event.

using System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;
 

namespace
DataTableDataColumnDataRow
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        DataTable dt;
        private void Form1_Load(object sender, EventArgs e)
        {
            DataColumn dc = new DataColumn();
            // Defining Datatype for Column Name
            dc.DataType=System.Type.GetType("System.String");
            // Defining Column Name
            dc.ColumnName = "Name";
            // Set Maximum Lenght for Column Text
            dc.MaxLength = 15;
            //
            //Repeate the same for other columns
            DataColumn dc1 = new DataColumn();
            dc1.DataType = System.Type.GetType("System.String");
            dc1.ColumnName = "City";
            dc1.MaxLength = 15;
 
            DataColumn dc2 = new DataColumn();
            dc2.DataType = System.Type.GetType("System.Int32");
            dc2.ColumnName = "Age";
         
 
            // Creating DataTable Object
            dt = new DataTable();
 
            //Adding columns to DataTable
            dt.Columns.Add(dc);
            dt.Columns.Add(dc1);
            dt.Columns.Add(dc2);
 
            dataGridView1.DataSource = dt;
        }
    }
}

Run the application.

Output



You can also define and create instance of DataColumn class in single line. Look at below code for doing same
work as above.

using System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;
 

namespace
DataTableDataColumnDataRow
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        DataTable dt;
        private void Form1_Load(object sender, EventArgs e)
        {
            // Creating DataColumn object
            DataColumn dc = new DataColumn("Name",typeof(string));
            DataColumn dc1 = new DataColumn("City",typeof(string));
            DataColumn dc2 = new DataColumn("Age",typeof(int));
             
            // Creating DataTable Object
            dt = new DataTable();
 
            //Adding columns to DataTable
            dt.Columns.Add(dc);
            dt.Columns.Add(dc1);
            dt.Columns.Add(dc2);
 
            dataGridView1.DataSource = dt;
        }
    }
}
 
Run the application.

Output



DataRow Class

The DataRow class represent the row of DatatTable.

Add a button and write the following code on Button Click event.

using System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;
 

namespace
DataTableDataColumnDataRow
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        DataTable dt;
        private void Form1_Load(object sender, EventArgs e)
        {
            // Creating DataColumn object
            DataColumn dc = new DataColumn("Name",typeof(string));
            DataColumn dc1 = new DataColumn("City",typeof(string));
            DataColumn dc2 = new DataColumn("Age",typeof(int));
             
            // Creating DataTable Object
            dt = new DataTable();

            //Adding columns to DataTable
            dt.Columns.Add(dc);
            dt.Columns.Add(dc1);
            dt.Columns.Add(dc2);
 
            dataGridView1.DataSource = dt;
        }
 
       
private void btnaddrow_Click(object sender, EventArgs e)

        {
            DataRow dr = dt.NewRow();

            dr[0] = "Alok";
            dr[1] = "Delhi";
            dr[2] = 23;
            dt.Rows.Add(dr);
            dataGridView1.DataSource = dt;
        }

    }
}
 
Run the application.

Output
 
 

Click the button.



You can also write the code of button click in another way. Look at below code which perform the same work as above code.

using System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;
 

namespace
DataTableDataColumnDataRow
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        DataTable dt;
        private void Form1_Load(object sender, EventArgs e)
        {            // Creating DataColumn object
            DataColumn dc = new DataColumn("Name",typeof(string));
            DataColumn dc1 = new DataColumn("City",typeof(string));
            DataColumn dc2 = new DataColumn("Age",typeof(int));
             
            // Creating DataTable Object
            dt = new DataTable();
 
            //Adding columns to DataTable
            dt.Columns.Add(dc);
            dt.Columns.Add(dc1);
            dt.Columns.Add(dc2);
 
            dataGridView1.DataSource = dt;
        }
 
       
private void btnaddrow_Click(object sender, EventArgs e)

        {
            DataRow dr = dt.NewRow();
            dr["Name"] = "Alok";
            dr["City"] = "Delhi";
            dr["Age"] = 23;
            dt.Rows.Add(dr);
            dataGridView1.DataSource = dt;
        }

    }
}

Output

Same output as above.

Resources

Here are some related resources