Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
DataRowState
using System;
using System.Data;
namespace DataTableRowState
{
public class BooksList
{
public static void Main(string[] args)
{
//This is the Datatable which contains book records.
DataTable dtBooks = new DataTable();
dtBooks.Columns.Add("Name", typeof(string));
dtBooks.Columns.Add("Id", typeof(Int32));
dtBooks.Columns.Add("Price", typeof(decimal));
//Taking Id as a primary key column and setting it to AutoIncrement.
dtBooks.PrimaryKey = new DataColumn[] { (dtBooks.Columns["Id"]) };
dtBooks.Columns["Id"].AutoIncrement = true;
dtBooks.Columns["Id"].AutoIncrementSeed = 1;//This is the initial primary key value
dtBooks.Columns["Id"].AutoIncrementStep = 1;//This is the incremeted value.
//Inserting Records.
DataRow drBook1 = dtBooks.NewRow();
drBook1["Name"] = "The Last Lecture";
drBook1["Price"] = 299;
dtBooks.Rows.Add(drBook1);
DataRow drBook2 = dtBooks.NewRow();
drBook2["Name"] = "Leader who had no title";
drBook2["Price"] = 199;
dtBooks.Rows.Add(drBook2);
DataRow drBook3 = dtBooks.NewRow();
drBook3["Name"] = "The Roots";
drBook3["Price"] = 599;
dtBooks.Rows.Add(drBook3);
DataRow drBook4 = dtBooks.NewRow();
drBook4["Name"] = "The Secret";
drBook4["Price"] = 500;
dtBooks.Rows.Add(drBook4);
DataRow drBook5 = dtBooks.NewRow();
drBook5["Name"] = "Tuesdays with Moorie";
drBook5["Price"] = 250;
dtBooks.Rows.Add(drBook5);
Console.WriteLine("==== Initial Book List ==== \n");
Console.WriteLine("ID\tPRICE \t NAME");
Console.WriteLine("---------------------------------------------");
for (int i = 0; i < dtBooks.Rows.Count; i++)
{
Console.WriteLine(dtBooks.Rows[i]["Id"].ToString() + "\t"
+ dtBooks.Rows[i]["Price"].ToString() + "\t "
+ dtBooks.Rows[i]["Name"].ToString());
}
Console.WriteLine("\n");
Console.ReadLine();
//Commits all the changes made to this table since the last time AcceptChanges was called.
//Here all the previously added rows state will be changed from Added to Unchanged.
dtBooks.AcceptChanges();
//Modified Row.
dtBooks.Rows[0]["Name"] = "The Last Lecture By Raundy Paush";
dtBooks.Rows[0]["Price"] = 499;
//Added Row.
DataRow drBook6 = dtBooks.NewRow();
drBook6["Name"] = "A Better India A Better World";
drBook6["Price"] = 399;
dtBooks.Rows.Add(drBook6);
Console.WriteLine("==== Book List After Changes ==== \n");
Console.WriteLine("ID\tPRICE \t NAME");
Console.WriteLine("---------------------------------------------");
for (int i = 0; i < dtBooks.Rows.Count; i++)
{
Console.WriteLine(dtBooks.Rows[i]["Id"].ToString() + "\t"
+ dtBooks.Rows[i]["Price"].ToString() + "\t "
+ dtBooks.Rows[i]["Name"].ToString());
}
Console.WriteLine("\n");
Console.ReadLine();
DataTable dtAdded = dtBooks.GetChanges(DataRowState.Added);
if (dtAdded != null)
{
Console.WriteLine("==== Newly Added Books in the Book List==== \n");
Console.WriteLine("ID\tPRICE \t NAME");
Console.WriteLine("---------------------------------------------");
for (int rowIndex = 0; rowIndex < dtAdded.Rows.Count; rowIndex++)
{
Console.WriteLine(dtAdded.Rows[rowIndex]["Id"].ToString() + "\t"
+ dtAdded.Rows[rowIndex]["Price"].ToString() + "\t "
+ dtAdded.Rows[rowIndex]["Name"].ToString());
}
Console.WriteLine("\n");
}
Console.ReadLine();
DataTable dtModified = dtBooks.GetChanges(DataRowState.Modified);
if (dtModified != null)
{
Console.WriteLine("==== Modified Books in the Book List ==== \n");
Console.WriteLine("ID\tPRICE \t NAME");
Console.WriteLine("---------------------------------------------");
for (int rowIndex = 0; rowIndex < dtModified.Rows.Count; rowIndex++)
{
Console.WriteLine(dtModified.Rows[rowIndex]["Id"].ToString() + "\t"
+ dtModified.Rows[rowIndex]["Price"].ToString() + "\t "
+ dtModified.Rows[rowIndex]["Name"].ToString());
}
Console.WriteLine("\n");
}
Console.ReadLine();
DataTable dtUnChanged = dtBooks.GetChanges(DataRowState.Unchanged);
if (dtUnChanged != null)
{
Console.WriteLine("==== UnChanged Books in the Book List ==== \n");
Console.WriteLine("ID\tPRICE \t NAME");
Console.WriteLine("---------------------------------------------");
for (int rowIndex = 0; rowIndex < dtUnChanged.Rows.Count; rowIndex++)
{
Console.WriteLine(dtUnChanged.Rows[rowIndex]["Id"].ToString() + "\t"
+ dtUnChanged.Rows[rowIndex]["Price"].ToString() + "\t "
+ dtUnChanged.Rows[rowIndex]["Name"].ToString());
}
Console.WriteLine("\n");
}
Console.ReadLine();
//In this case we can not access the deleted rows, so we are printing it first and then deleting.
Console.WriteLine("==== Deleted Books in the Book List ==== \n");
Console.WriteLine("ID\tPRICE \t NAME");
Console.WriteLine("---------------------------------------------");
Console.WriteLine(drBook2["Id"].ToString() + "\t"
+ drBook2["Price"].ToString() + "\t "
+ drBook2["Name"].ToString());
dtBooks.Rows[1].Delete();
Console.WriteLine("\n");
Console.ReadLine();
//In this case we can not access the removed rows, so we are printing it first and then removing.
Console.WriteLine("==== Detached Books in the Book List ==== \n");
Console.WriteLine("ID\tPRICE \t NAME");
Console.WriteLine("---------------------------------------------");
Console.WriteLine(drBook5["Id"].ToString() + "\t" + drBook5["Price"].ToString() + "\t " + drBook5["Name"].ToString());
dtBooks.Rows.Remove(drBook5);
Console.WriteLine("\n");
Console.ReadLine();
Console.WriteLine("==== Book List After Rejecting all the changes ==== \n");
Console.WriteLine("ID\tPRICE \t NAME");
Console.WriteLine("---------------------------------------------");
dtBooks.RejectChanges();
for (int i = 0; i < dtBooks.Rows.Count; i++)
{
Console.WriteLine(dtBooks.Rows[i]["Id"].ToString() + "\t"
+ dtBooks.Rows[i]["Price"].ToString() + "\t "
+ dtBooks.Rows[i]["Name"].ToString());
}
Console.WriteLine("\n");
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment