Working with DataTable Deleted Rows

G

Guest

I'm working in Visual Studio 2005 and C#.

I have a DataTable with many rows. The user can delete a row. After the row
is deleted, I'd like to column values for the deleted row, but any attempt to
access column values results in the following exception:

{"Deleted row information cannot be accessed through the row."}

My understanding is that a copy of the original row is still in there. Any
suggestions on how to access??

Thanks,
Randy

DataViewRowState dvrs = DataViewRowState.Deleted;
DataRow[] rows = MyDataSet.MyDataTable.Select("", "", dvrs);
for (int i = 0; i < rows.Length; i++)
{
DataRow dr = rows;
Console.WriteLine(dr["Column42"].ToString());
}
 
G

Guest

You could try something like this:

DataTable deletedRows = mTTSDataSet1.Table1.GetChanges(DataRowState.Deleted);
for (int i = 0; i < deletedRows.Rows.Count; i++)
{
Console.WriteLine("Deleted: " + deletedRows.Rows["Column2",
DataRowVersion.Original].ToString());
}

Adrian.
 
G

Guest

Yes, that was very helpful. Thanks!

Any suggestion on how to extract the entire original row? The following
variation doesn't quite work.

DataRow dr = deletedRows.Rows[DataRowVersion.Original].ToString());

Thanks again,
Randy



Adrian Voicu said:
You could try something like this:

DataTable deletedRows = mTTSDataSet1.Table1.GetChanges(DataRowState.Deleted);
for (int i = 0; i < deletedRows.Rows.Count; i++)
{
Console.WriteLine("Deleted: " + deletedRows.Rows["Column2",
DataRowVersion.Original].ToString());
}

Adrian.
--
[Please mark my answer if it was helpful to you]




randy1200 said:
I'm working in Visual Studio 2005 and C#.

I have a DataTable with many rows. The user can delete a row. After the row
is deleted, I'd like to column values for the deleted row, but any attempt to
access column values results in the following exception:

{"Deleted row information cannot be accessed through the row."}

My understanding is that a copy of the original row is still in there. Any
suggestions on how to access??

Thanks,
Randy

DataViewRowState dvrs = DataViewRowState.Deleted;
DataRow[] rows = MyDataSet.MyDataTable.Select("", "", dvrs);
for (int i = 0; i < rows.Length; i++)
{
DataRow dr = rows;
Console.WriteLine(dr["Column42"].ToString());
}
 
G

Guest

The following line of code will pretty much give you a table with all the
deleted rows.

DataTable delRowsTable = mTTSDataSet1.Table1.GetChanges(DataRowState.Deleted);

Using this you can parse each row and display the original contents of a
cell by specifing a column as in the previous example. You could use this
information to build a query or record data that was destroyed.

The following code

DataRow dr = delRowsTable.Rows[DataRowVersion.Original].ToString())

does not work because "DataRowVersion.Original" is supposed to be the name
of a column in the ith deleted row. The result will be the value in the ith
row and the column you specified that you are then trying to assign to a
DataRow object.

Adrian.
--
[Please mark my answer if it was helpful to you]




randy1200 said:
Yes, that was very helpful. Thanks!

Any suggestion on how to extract the entire original row? The following
variation doesn't quite work.

DataRow dr = deletedRows.Rows[DataRowVersion.Original].ToString());

Thanks again,
Randy



Adrian Voicu said:
You could try something like this:

DataTable deletedRows = mTTSDataSet1.Table1.GetChanges(DataRowState.Deleted);
for (int i = 0; i < deletedRows.Rows.Count; i++)
{
Console.WriteLine("Deleted: " + deletedRows.Rows["Column2",
DataRowVersion.Original].ToString());
}

Adrian.
--
[Please mark my answer if it was helpful to you]




randy1200 said:
I'm working in Visual Studio 2005 and C#.

I have a DataTable with many rows. The user can delete a row. After the row
is deleted, I'd like to column values for the deleted row, but any attempt to
access column values results in the following exception:

{"Deleted row information cannot be accessed through the row."}

My understanding is that a copy of the original row is still in there. Any
suggestions on how to access??

Thanks,
Randy

DataViewRowState dvrs = DataViewRowState.Deleted;
DataRow[] rows = MyDataSet.MyDataTable.Select("", "", dvrs);
for (int i = 0; i < rows.Length; i++)
{
DataRow dr = rows;
Console.WriteLine(dr["Column42"].ToString());
}
 
G

Guest

Many thanks for both excellent responses (both maked yes as helpful). On the
second question, I just found that the following also works for getting a
table with the original versions of the deleted rows.

//The DataView (dv) now contains only deleted rows
DataView dv = new DataView( TableWithDeletedRows,
null, null, DataViewRowState.Deleted);
//The new DataTable (dt) now contains the original versions of the deleted
rows.
DataTable dt = dv.ToTable();

Thanks again for all the help!
Randy

Adrian Voicu said:
The following line of code will pretty much give you a table with all the
deleted rows.

DataTable delRowsTable = mTTSDataSet1.Table1.GetChanges(DataRowState.Deleted);

Using this you can parse each row and display the original contents of a
cell by specifing a column as in the previous example. You could use this
information to build a query or record data that was destroyed.

The following code

DataRow dr = delRowsTable.Rows[DataRowVersion.Original].ToString())

does not work because "DataRowVersion.Original" is supposed to be the name
of a column in the ith deleted row. The result will be the value in the ith
row and the column you specified that you are then trying to assign to a
DataRow object.

Adrian.
--
[Please mark my answer if it was helpful to you]




randy1200 said:
Yes, that was very helpful. Thanks!

Any suggestion on how to extract the entire original row? The following
variation doesn't quite work.

DataRow dr = deletedRows.Rows[DataRowVersion.Original].ToString());

Thanks again,
Randy



Adrian Voicu said:
You could try something like this:

DataTable deletedRows = mTTSDataSet1.Table1.GetChanges(DataRowState.Deleted);
for (int i = 0; i < deletedRows.Rows.Count; i++)
{
Console.WriteLine("Deleted: " + deletedRows.Rows["Column2",
DataRowVersion.Original].ToString());
}

Adrian.
--
[Please mark my answer if it was helpful to you]




:

I'm working in Visual Studio 2005 and C#.

I have a DataTable with many rows. The user can delete a row. After the row
is deleted, I'd like to column values for the deleted row, but any attempt to
access column values results in the following exception:

{"Deleted row information cannot be accessed through the row."}

My understanding is that a copy of the original row is still in there. Any
suggestions on how to access??

Thanks,
Randy

DataViewRowState dvrs = DataViewRowState.Deleted;
DataRow[] rows = MyDataSet.MyDataTable.Select("", "", dvrs);
for (int i = 0; i < rows.Length; i++)
{
DataRow dr = rows;
Console.WriteLine(dr["Column42"].ToString());
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top