Compare DataTables

I am getting data from a database and putting it in a DataTable.  I then bind the datatable to a gridview.  I also set the datatable to another datatable named _datatable.  

I want to compare the datatable in the gridvew and _datatable to see if the data changed.  What is the easiest way to do this?

How do you extract the datasource of a gridview to a dataset?
LVL 1
CipherISAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
ajb2222Connect With a Mentor Commented:
Since this is a web application,  you want to use the gridview events.

the RowUpdating event will allow you to check the data being updated and cancel the update if you need to.

this example will cancel the update if the first column has beeen changed to zero

       protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            DataTable dt = (DataTable)Session["MyData"];
            for(int x = 0; x < dt.Columns.Count; dt++)
            if (e.NewValues[0] != e.OldValues[0])
            {
                if (e.NewValues[0] = 0)

                    e.Cancel = true;

            }

        }

Open in new window


0
 
ajb2222Commented:
Why,

a datatable keeps track of changes.

The rowstate property of each datarow lets you know if there where changes to the datarow

you can look at the original data in a datarows column like this dtr.Rows[x]["columname",DataRowVersion.Original]
0
 
CipherISAuthor Commented:
how do i set dtr = gridview.datasource?
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
ajb2222Commented:
dtr is your original datatable

but you can also use

DataTable dtr = (DataTable)GridView1.DataSource;

where Gridview1 is the name of your GridView
0
 
s_chilkuryCommented:
///first convert gridview data to DataTable

then

////Code to Compare 2 DataTables and return a 3rd DataTable with difference

public DataTable CompareDataTables(DataTable first, DataTable second)
{
first.TableName = "FirstTable";
second.TableName = "SecondTable";

//Create Empty Table
DataTable table = new DataTable("Difference");

try
{
//Must use a Dataset to make use of a DataRelation object
using (DataSet ds = new DataSet())
{
//Add tables
ds.Tables.AddRange(new DataTable[] { first.Copy(), second.Copy() });

//Get Columns for DataRelation
DataColumn[] firstcolumns = new DataColumn[ds.Tables[0].Columns.Count];

for (int j = 0; j < firstcolumns.Length; j++)
{
firstcolumns[j] = ds.Tables[0].Columns[j];
}

DataColumn[] secondcolumns = new DataColumn[ds.Tables[1].Columns.Count];

for (int j = 0; j < secondcolumns.Length; j++)
{
secondcolumns[j] = ds.Tables[1].Columns[j];
}

//Create DataRelation
DataRelation r = new DataRelation(string.Empty, firstcolumns, secondcolumns, false);

ds.Relations.Add(r);

//Create columns for return table
for (int j = 0; j < first.Columns.Count; j++)
{
table.Columns.Add(first.Columns[j].ColumnName, first.Columns[j].DataType);
}

//If First Row not in Second, Add to return table.
table.BeginLoadData();

foreach (DataRow parentrow in ds.Tables[0].Rows)
{
DataRow[] childrows = parentrow.GetChildRows(r);
if (childrows == null || childrows.Length == 0)
table.LoadDataRow(parentrow.ItemArray, true);
}

table.EndLoadData();

}
}
catch (Exception ex)
{
throw ex;
}

return table;
}


0
 
CipherISAuthor Commented:
i'm having issues converting the gridview to datatable.

When I set the gridvdiew datasource to a datatable I get a null (see pic below)
datatable.jpg
0
 
CipherISAuthor Commented:
how do i set viewstate to the gridview?
0
 
ajb2222Commented:
Where do you originaly set the gvNotifications.Datasource?

You should have a line somewhere that looks something like this

dim dt as datatable;
// fill datatable here
gvNotifications.Datasource = dt;

If you set the datasource equal to some other type like a dataset,  you can only cast it back to that datatype, otherwise you get null.

0
 
CipherISAuthor Commented:
DataTable dt = Notification.GetNotifications();  //Returns a DataTable
this.gvNotification.DataSource = dt;
0
 
CipherISAuthor Commented:
DataTable dt = Notification.GetNotifications();  //Returns a DataTable
this.gvNotification.DataSource = dt;
this.gvNotification.DataBind();
0
 
ajb2222Commented:
The databind()  tells me this is a web page.  Now I know Why you are trying to get the datatable back from the gridview.

What you need to do is store the datatable in a session variable like this

Session["MyData"] = dt;

and to get it back

dt = (DataTable)Session["MyData"];
0
 
CipherISAuthor Commented:
This does not resolve my problem.  I can store the datatable but I need to get the changes from the gridview.  the gridview is not bound to the database.  data in the gridview is set by a datatable.  

Once the datatable is passed to the gridview i need to know what data changed.
0
 
ajb2222Commented:
Since you are not using an SQLDatasource the grid will not make the actual changes to your datatable.  

I would perform these in the RowUpdated event of the gridview wich fires after the row of the gridview has been updated.  It will not fire if the update has been canceled in RowUpdating.
0
 
CipherISAuthor Commented:
i resolved the issue.  will award points for effort.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.