[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Compare DataTables

Posted on 2010-11-15
14
Medium Priority
?
859 Views
Last Modified: 2012-05-10
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?
0
Comment
Question by:CipherIS
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
14 Comments
 
LVL 6

Expert Comment

by:ajb2222
ID: 34136296
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
 
LVL 1

Author Comment

by:CipherIS
ID: 34136363
how do i set dtr = gridview.datasource?
0
 
LVL 6

Expert Comment

by:ajb2222
ID: 34136466
dtr is your original datatable

but you can also use

DataTable dtr = (DataTable)GridView1.DataSource;

where Gridview1 is the name of your GridView
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:s_chilkury
ID: 34136632
///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
 
LVL 1

Author Comment

by:CipherIS
ID: 34136666
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
 
LVL 1

Author Comment

by:CipherIS
ID: 34136876
how do i set viewstate to the gridview?
0
 
LVL 6

Expert Comment

by:ajb2222
ID: 34136926
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
 
LVL 1

Author Comment

by:CipherIS
ID: 34136985
DataTable dt = Notification.GetNotifications();  //Returns a DataTable
this.gvNotification.DataSource = dt;
0
 
LVL 1

Author Comment

by:CipherIS
ID: 34136995
DataTable dt = Notification.GetNotifications();  //Returns a DataTable
this.gvNotification.DataSource = dt;
this.gvNotification.DataBind();
0
 
LVL 6

Expert Comment

by:ajb2222
ID: 34137721
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
 
LVL 1

Author Comment

by:CipherIS
ID: 34137781
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
 
LVL 6

Accepted Solution

by:
ajb2222 earned 2000 total points
ID: 34138155
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
 
LVL 6

Expert Comment

by:ajb2222
ID: 34138204
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
 
LVL 1

Author Comment

by:CipherIS
ID: 34139502
i resolved the issue.  will award points for effort.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question