Working with DataTable and its Methods in ADO.NET


Introduction

A DataTable is a class of Disconnected architecture in the .NET Framework. It is found in the System.Data namespace and represents a single table. We can create a table and can add columns and rows of that table. Now, we will create an window application and use the DataTable class.

Create a windows application > create a dataGridView control and write the following code on the 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
datatable
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        DataTable dt;
        private void Form1_Load(object sender, EventArgs e)
        {
            //Adding Columns
            dt = new DataTable();
            dt.Columns.Add("ID", typeof(int));
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Department",System.Type.GetType("System.String"));
            dt.Columns.Add("City", typeof(string));
            //Adding Rows
            dt.Rows.Add(111, "Alok", "IT", "Delhi");
            dt.Rows.Add(222, "Satish", "IT", "Jhansi");
            dt.Rows.Add(333, "Amitabh", "Management", "Noida");           
        } 
       
    }
}
 

Run the application.
Output



It is a simple example for creating a datatable and adding column and value to its row. Now we work with its different method.

Working with it's methods


AcceptChanges()

This method saves changes which made with records in a Datatable.
Take another dataGridView control and a Button. Add below code.

       
private void btnacceptchanges_Click(object sender, EventArgs e)
        {
            dt.AcceptChanges();
            dataGridView2.DataSource = dt;
        }

Run the application. Make some changes in rows value. I have changed department name as "cs" for first row and added a new row at last.

Output



Click the
"AcceptChanges" button. Second dataGridView will show updated record.



Clear()

This method clears (removes) all data from datatable. Take a button and write the following code on click event.

       
private void btnclear_Click(object sender, EventArgs e)
        {
            dt.Clear();
        }

Output
 

Click the "Clear" button. It will clear all the data.

Output


Clone()

The clone method copy the structure of Datatable. Means it copy only schema not full records of Datatable.
Take another dataGridView control and a Button. Add below code.

       
private void btnclone_Click(object sender, EventArgs e)
        {
            DataTable dt1 = dt.Clone();
            dataGridView2.DataSource = dt1;
        }

Output



Click the "Clone" button.

Output



Copy()

The copy method copy the whole records with structure of Datatable. Take another dataGridView control and a Button. Write the following code on button click.

       
private void btncopy_Click(object sender, EventArgs e)
        {
            DataTable dt1 = dt.Copy();
            dataGridView2.DataSource = dt1;
        }

Output



Click the "copy" button. It will copy all the data with structure of datatable into another datatable and show records in dataGridView.

Output



GetChanges()

This method does copy of all records of datatable with changes made with record. Add a button and dataGridView and
write the following code on button click.

       
private void btnGetChanges_Click(object sender, EventArgs e)
        {
           
dt.GetChanges();
            dataGridView2.DataSource = dt;

        }

Output

I have changed some column value like department as "CS" of first row and city as "Varanasi" for last row.

 

Click the "GetChanges" button. It will show all record.
Output



NewRow()

The NewRow method creates a new row with same schema of datatable. Take a button > Set it's Text property to "NewRow" and write the following code.

        private void btnnewrow_Click(object sender, EventArgs e)
        {
            DataRow dr = dt.NewRow();
            dr["ID"] = 444;
            dr["Name"] = "Pramod";
            dr["Department"]="CS";
            dr["City"] = "Allahabad";
            //Add the row to datatable
            dt.Rows.Add(dr);
        }

Run the application.

Output



Click the "NewRow" button. It will add new row to datatable.

Output

 


Similar Articles