How to remove rows from a DataTable without deleting form thedatabase

  • Thread starter Thread starter Susan Mackay
  • Start date Start date
S

Susan Mackay

I have a data table that is connected to a database table with a data
adapter in the 'standard' manner.

However I want to be able to remove selected rows from the data table (i.e.
no longer include them in the set that is displayed to the user) but I don't
want to delete the corresponding row from the database.

I've tried using the .Rows.Remove() method but an exception is thrown to say
I don't have a 'delete' command in the data adapter (which I don't of
course).

Because of the way the 'user interface' works, I can't build up an SQL
string that represents the current set of records (the user can "add to" and
"remove from" the current set of records - there is no underlying SQL
statement that represents the totality of the users additions and removals)
so I can't just clear the table and re-fill it.

Is there a way to do this?

Thanks

Susan
 
Susan,

Can you show us the piece of code that you use to remove, because in my idea
are you using the right approach. The remove should not affect any
dataadapter, it just completely removes a datarow from a datatable (as long
as it is not a parent row in a relation).

Cor
 
Cor,

The relevant part is:
(Filterstring is a StringBuilder instance containing the row selection
statement, JobDataTable is the datatable in question)

DataRow[] TargetRows = JobDataTable.Select( FilterString.ToString());

foreach( DataRow TargetRow in TargetRows)
{ JobDataTable.Rows.Remove( TargetRow); }


You may have already answered the question: the JobDataTable is the parent
of two relationships with other tables in the DataSet - JobAttributes and
JobContacts. Each is set up by code such as the following:
(DatabaseDS is the dataset that holds all of the tables)

DatabaseDS.Relations.Add("JobsAttributes",
DatabaseDS.Tables["Jobs"].Columns["InternalJobID"],
DatabaseDS.Tables["JobAttributes"].Columns["InternalJobID"]);

The 'child' tables may or may not contain any records that relate to a
parent record.

I have a BindingSource instance that links a DataGridView with the data
table. The BindingSource has a CurrentChanged event on it, and the event
code updates the database if anything has changed (it then goes on to load
the child tables if they do not already have the appropriate records for the
new parent record - this stops [in some cases] hundreds of records being
read that will never be displayed/used). It is during the database update
that the exception is triggered.

The only other thing that might be relevant is that the parent table
(JobsDataTable) has a computed column:
JobDataTable.Columns.Add("MaxJobStatus", typeof(int),
"iif(Count(Child(JobsContacts).JobStatus)=0,JobStatus,Max(Child(JobsContacts
).JobStatus))");


Thanks for the help - it is much appreciated

Susan
 
Susan,

First the last part, the update is only affecting those columns that are in
the SQL transaction code of that.
(Stored procedure or commandText).

However are you sure that that DataRow Array TargetRows is filled.
I become suspicious seeing that FilterString.ToString(), the Select string
is not the most fine one to make.

Cor


Susan Mackay said:
Cor,

The relevant part is:
(Filterstring is a StringBuilder instance containing the row selection
statement, JobDataTable is the datatable in question)

DataRow[] TargetRows = JobDataTable.Select( FilterString.ToString());

foreach( DataRow TargetRow in TargetRows)
{ JobDataTable.Rows.Remove( TargetRow); }


You may have already answered the question: the JobDataTable is the parent
of two relationships with other tables in the DataSet - JobAttributes and
JobContacts. Each is set up by code such as the following:
(DatabaseDS is the dataset that holds all of the tables)

DatabaseDS.Relations.Add("JobsAttributes",
DatabaseDS.Tables["Jobs"].Columns["InternalJobID"],
DatabaseDS.Tables["JobAttributes"].Columns["InternalJobID"]);

The 'child' tables may or may not contain any records that relate to a
parent record.

I have a BindingSource instance that links a DataGridView with the data
table. The BindingSource has a CurrentChanged event on it, and the event
code updates the database if anything has changed (it then goes on to load
the child tables if they do not already have the appropriate records for
the
new parent record - this stops [in some cases] hundreds of records being
read that will never be displayed/used). It is during the database update
that the exception is triggered.

The only other thing that might be relevant is that the parent table
(JobsDataTable) has a computed column:
JobDataTable.Columns.Add("MaxJobStatus", typeof(int),
"iif(Count(Child(JobsContacts).JobStatus)=0,JobStatus,Max(Child(JobsContacts
).JobStatus))");


Thanks for the help - it is much appreciated

Susan


Susan,

Can you show us the piece of code that you use to remove, because in my
idea
are you using the right approach. The remove should not affect any
dataadapter, it just completely removes a datarow from a datatable (as
long
as it is not a parent row in a relation).

Cor
 
Cor,

I've stepped through this with the debugger and I am certain that (at least
in the test case I used) the TargetRows array DOES contain records. Also, if
it didn't, then the 'foreach' loop would not be entered and the 'Remove'
method would not be called (that's my understanding anyway!).

I have not included the code that builds the FilterString, but, again at
least in the test case I stepped through, it did contain a valid expression
and the problem still occurred.

Thanks for the continuing support

Susan




Susan,

First the last part, the update is only affecting those columns that are in
the SQL transaction code of that.
(Stored procedure or commandText).

However are you sure that that DataRow Array TargetRows is filled.
I become suspicious seeing that FilterString.ToString(), the Select string
is not the most fine one to make.

Cor


Susan Mackay said:
Cor,

The relevant part is:
(Filterstring is a StringBuilder instance containing the row selection
statement, JobDataTable is the datatable in question)

DataRow[] TargetRows = JobDataTable.Select( FilterString.ToString());

foreach( DataRow TargetRow in TargetRows)
{ JobDataTable.Rows.Remove( TargetRow); }

.....
 
Susan,

Can you try it like this

for (int i = TargetRows.Count; i > 0; i--)
{DataRow TargetRow = TargetRows;
TargetRow.Remove; }

The TargetRow is AFAIK only a referencetable to your table.

Check the routine, because I have typed this in here. I do this bottom up
looping everytime in another way. (Maybe does the foreach does it as well,
but I did not check it therefore this one, but now maybe I have two answers
in one message)

I do it like that, because collections will sometimes become in trouble when
you start removing a first one as first.

Cor




Susan Mackay said:
Cor,

The relevant part is:
(Filterstring is a StringBuilder instance containing the row selection
statement, JobDataTable is the datatable in question)

DataRow[] TargetRows = JobDataTable.Select( FilterString.ToString());

foreach( DataRow TargetRow in TargetRows)
{ JobDataTable.Rows.Remove( TargetRow); }


You may have already answered the question: the JobDataTable is the parent
of two relationships with other tables in the DataSet - JobAttributes and
JobContacts. Each is set up by code such as the following:
(DatabaseDS is the dataset that holds all of the tables)

DatabaseDS.Relations.Add("JobsAttributes",
DatabaseDS.Tables["Jobs"].Columns["InternalJobID"],
DatabaseDS.Tables["JobAttributes"].Columns["InternalJobID"]);

The 'child' tables may or may not contain any records that relate to a
parent record.

I have a BindingSource instance that links a DataGridView with the data
table. The BindingSource has a CurrentChanged event on it, and the event
code updates the database if anything has changed (it then goes on to load
the child tables if they do not already have the appropriate records for
the
new parent record - this stops [in some cases] hundreds of records being
read that will never be displayed/used). It is during the database update
that the exception is triggered.

The only other thing that might be relevant is that the parent table
(JobsDataTable) has a computed column:
JobDataTable.Columns.Add("MaxJobStatus", typeof(int),
"iif(Count(Child(JobsContacts).JobStatus)=0,JobStatus,Max(Child(JobsContacts
).JobStatus))");


Thanks for the help - it is much appreciated

Susan


Susan,

Can you show us the piece of code that you use to remove, because in my
idea
are you using the right approach. The remove should not affect any
dataadapter, it just completely removes a datarow from a datatable (as
long
as it is not a parent row in a relation).

Cor
 
Cor,

I can't exactly as you requested because the DataRow itself does not have a
Remove() method - the method belongs to the DataRowCollection referenced by
(in this case) JobDataTable.Rows.

I can see what you are getting at and I've had this problem in other
contexts - removing the early elements form a collection upsets the indexing
for all subsequent items. This is also why the 'foreach' enumerator is
effectively read only.

What does work is:

DataRow[] TargetRows = JobDataTable.Select( FilterString.ToString());
for( int Index = TargetRows.Length - 1; Index >= 0; Index--)
{
DataRow TargetRow = TargetRow[Index];
JobDataTable.Remove( TargetRow);
}

Actually the code as I originally presented it also works because I've found
the problem: and its not directly related to anything I mentioned in this
thread (isn't it always the way - you never get all of the information!!!).

The problem occurs when the 'current' row (as seen by a BindingSource linked
to the data table) is removed from the data table. As mentioned in the
original posting, I have an CurrentChanged event on this BindingSource.

In this case, when the 'current' record is removed, the binding source
naturally selects a new 'current' record and triggers the event which tries
to update the database with any changes. Probably because of some internal
operation sequence within the Remove() method and the event processing of
the Binding Source, the update procedure must still see the 'deleted' record
still in the collection and tries to delete the record from the database.

Having found the problem, I've now decided on another approach to this whole
thing which avoids any attempts to remove the 'current' record and
everything works nicely.

So thank you for your help and for helping me to more fully understand what
is going on.

Cheers

Susan



Susan,

Can you try it like this

for (int i = TargetRows.Count; i > 0; i--)
{DataRow TargetRow = TargetRows;
TargetRow.Remove; }

The TargetRow is AFAIK only a referencetable to your table.

Check the routine, because I have typed this in here. I do this bottom up
looping everytime in another way. (Maybe does the foreach does it as well,
but I did not check it therefore this one, but now maybe I have two answers
in one message)

I do it like that, because collections will sometimes become in trouble when
you start removing a first one as first.

Cor




Susan Mackay said:
Cor,

The relevant part is:
(Filterstring is a StringBuilder instance containing the row selection
statement, JobDataTable is the datatable in question)

DataRow[] TargetRows = JobDataTable.Select( FilterString.ToString());

foreach( DataRow TargetRow in TargetRows)
{ JobDataTable.Rows.Remove( TargetRow); }


You may have already answered the question: the JobDataTable is the parent
of two relationships with other tables in the DataSet - JobAttributes and
JobContacts. Each is set up by code such as the following:
(DatabaseDS is the dataset that holds all of the tables)

DatabaseDS.Relations.Add("JobsAttributes",
DatabaseDS.Tables["Jobs"].Columns["InternalJobID"],
DatabaseDS.Tables["JobAttributes"].Columns["InternalJobID"]);

The 'child' tables may or may not contain any records that relate to a
parent record.

I have a BindingSource instance that links a DataGridView with the data
table. The BindingSource has a CurrentChanged event on it, and the event
code updates the database if anything has changed (it then goes on to load
the child tables if they do not already have the appropriate records for
the
new parent record - this stops [in some cases] hundreds of records being
read that will never be displayed/used). It is during the database update
that the exception is triggered.

The only other thing that might be relevant is that the parent table
(JobsDataTable) has a computed column:
JobDataTable.Columns.Add("MaxJobStatus", typeof(int),
"iif(Count(Child(JobsContacts).JobStatus)=0,JobStatus,Max(Child(JobsContacts
).JobStatus))");


Thanks for the help - it is much appreciated

Susan


Susan,

Can you show us the piece of code that you use to remove, because in my
idea
are you using the right approach. The remove should not affect any
dataadapter, it just completely removes a datarow from a datatable (as
long
as it is not a parent row in a relation).

Cor

"Susan Mackay" <[email protected]> schreef in bericht
I have a data table that is connected to a database table with a data
adapter in the 'standard' manner.

However I want to be able to remove selected rows from the data table
(i.e.
no longer include them in the set that is displayed to the user) but I
don't
want to delete the corresponding row from the database.

I've tried using the .Rows.Remove() method but an exception is thrown to
say
I don't have a 'delete' command in the data adapter (which I don't of
course).

Because of the way the 'user interface' works, I can't build up an SQL
string that represents the current set of records (the user can "add to"
and
"remove from" the current set of records - there is no underlying SQL
statement that represents the totality of the users additions and
removals)
so I can't just clear the table and re-fill it.

Is there a way to do this?

Thanks

Susan
 
Back
Top