Reputation: 445
In my project. I have a datatable and a button named 'Process'
After clicked the 'Process' button, my datatable column changed to a new value (as i coded below)
Is it possible to get those changed value back (before i clicked the button) because i need to display them together with the new value (when i click the button)
Here's what i coded so far:
for (int l = 0; l < my_datatable.Rows.Count; l++)
{
data_source = my_datatable.Rows[l][3].ToString();
if (data_source.Contains("Cross Site Scripting"))
{
my_datatable.Rows[l][3] = "2";
}
else if (data_source.Contains("SQL Injection"))
{
my_datatable.Rows[l][3] = "3";
}
else if (data_source.Contains("Unicode Attack"))
{
my_datatable.Rows[l][3] = "4";
}
else if (data_source.Contains("Proxy Attack"))
{
my_datatable.Rows[l][3] = "5";
}
else
{
my_datatable.Rows[l][3] = "1";
}
}
Upvotes: 0
Views: 1142
Reputation: 2890
If you copy the datatable before you alter it you should be able to do it.
var dataCopy = my_datatable.Copy();
The only thing you'll have to do is to keep track of what fields in what row you you have changed.
Thinking about it, there may be a better solution using the some of the below. I can't recall the exact specifics without researching but they may be of some use.
if (row.RowState == DataRowState.Modified)
{
var currentRow = row[0, DataRowVersion.Current];
var previousRow = row[0, DataRowVersion.Proposed];
}
You can then use this to get both values
Or there is also
var modifiedTable = my_datatable.GetChanges(DataRowState.Modified)
Hello again... I've done a bit of experimentation and I believe the below will do what you need. It is based on a simple data table that I create to demonstrate.
var data = new DataTable("Table1");
data.Columns.Add(new DataColumn("Username", typeof(string)));
data.Columns.Add(new DataColumn("UserId", typeof(int)));
var row = data.NewRow();
row["Username"] = "User1";
row["Userid"] = 12356;
data.Rows.Add(row);
data.AcceptChanges();
var originalTable = data.Copy();
var rowToEdit = data.Rows[0];
rowToEdit["Username"] = "User2";
var rowPos = 0;
foreach (DataRow dr in data.Rows)
{
if (dr.RowState == DataRowState.Modified)
{
foreach (DataColumn c in dr.Table.Columns)
{
if (dr.ItemArray[c.Ordinal] != originalTable.Rows[rowPos].ItemArray[c.Ordinal] )
{
//Show Changed value
var x = originalTable.Rows[rowPos].ItemArray[c.Ordinal];
}
}
}
rowPos++;
}
Thanks
Upvotes: 1
Reputation: 1464
var dictionary = new Dictionary<int, string>();
for (int l = 0; l < my_datatable.Rows.Count; l++)
{
data_source = my_datatable.Rows[l][3].ToString();
dictionary.Add(l, data_source);
// ...
}
// Access:
dictionary[0] // Returns the previous string
I would choose to use a Dictionary<TKey, TValue>
to store the strings based on their row index within the DataTable
. By using the Dictionary<TKey, TValue>.Add(key, value)
method, it stores it for later usage.
When accessing the string values from it, you can treat it as an associative array and just use Dictionary<TKey, TValue>[index]
.
Hope this helped!
Upvotes: 0