By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,070 Members | 1,718 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,070 IT Pros & Developers. It's quick & easy.

How to remove rows from a DataTable without deleting form thedatabase

P: n/a
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

Sep 11 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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" <ma******@hotmail.comschreef in bericht
news:C1********************@hotmail.com...
>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

Sep 12 '06 #2

P: n/a
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,JobSta tus,Max(Child(JobsContacts
).JobStatus))");
Thanks for the help - it is much appreciated

Susan
On 12/9/06 14:07, in article eJ*************@TK2MSFTNGP06.phx.gbl, "Cor
Ligthert [MVP]" <no************@planet.nlwrote:
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" <ma******@hotmail.comschreef in bericht
news:C1********************@hotmail.com...
>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

Sep 12 '06 #3

P: n/a
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" <ma******@hotmail.comschreef in bericht
news:C1********************@hotmail.com...
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,JobSta tus,Max(Child(JobsContacts
).JobStatus))");
Thanks for the help - it is much appreciated

Susan
On 12/9/06 14:07, in article eJ*************@TK2MSFTNGP06.phx.gbl, "Cor
Ligthert [MVP]" <no************@planet.nlwrote:
>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" <ma******@hotmail.comschreef in bericht
news:C1********************@hotmail.com...
>>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


Sep 12 '06 #4

P: n/a
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


On 13/9/06 02:38, in article OI**************@TK2MSFTNGP03.phx.gbl, "Cor
Ligthert [MVP]" <no************@planet.nlwrote:
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" <ma******@hotmail.comschreef in bericht
news:C1********************@hotmail.com...
>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); }

.....
Sep 12 '06 #5

P: n/a
Susan,

Can you try it like this

for (int i = TargetRows.Count; i 0; i--)
{DataRow TargetRow = TargetRows[i];
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" <ma******@hotmail.comschreef in bericht
news:C1********************@hotmail.com...
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,JobSta tus,Max(Child(JobsContacts
).JobStatus))");
Thanks for the help - it is much appreciated

Susan
On 12/9/06 14:07, in article eJ*************@TK2MSFTNGP06.phx.gbl, "Cor
Ligthert [MVP]" <no************@planet.nlwrote:
>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" <ma******@hotmail.comschreef in bericht
news:C1********************@hotmail.com...
>>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


Sep 13 '06 #6

P: n/a
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

On 13/9/06 15:14, in article #z**************@TK2MSFTNGP04.phx.gbl, "Cor
Ligthert [MVP]" <no************@planet.nlwrote:
Susan,

Can you try it like this

for (int i = TargetRows.Count; i 0; i--)
{DataRow TargetRow = TargetRows[i];
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" <ma******@hotmail.comschreef in bericht
news:C1********************@hotmail.com...
>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,JobSt atus,Max(Child(JobsContacts
).JobStatus))");
Thanks for the help - it is much appreciated

Susan
On 12/9/06 14:07, in article eJ*************@TK2MSFTNGP06.phx.gbl, "Cor
Ligthert [MVP]" <no************@planet.nlwrote:
>>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" <ma******@hotmail.comschreef in bericht
news:C1********************@hotmail.com...
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

Sep 15 '06 #7

P: n/a
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.
http://msdn.microsoft.com/library/de...emovetopic.asp

http://msdn.microsoft.com/library/de...emovetopic.asp

I hope this helps,

Cor
Sep 15 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.