newbie question on deleting rows from DataTable

T

Tim Holgerson

Hi,

I am new to C# and to the .NET data environment. I am having some
trouble understanding some of the events around deleting rows.

Here is my problem:

I delete the row from a bound datagrid using a currency mananger:
CurrencyManager cm = (CurrencyManager)
this.dataGridEng.BindingContext[this.oEngClassObj.EngObjDataSet.Tables["EngClasses"]];
if ( !(cm.List is DataViewManager) && ( cm.Count >= 0 ) )
{
try
{
cm.RemoveAt( this.dataGridEng.CurrentRowIndex);

}
catch (Exception ew)
{

this.evParentExceptHandler(this,ew);
}
}

this seems to work and the row is deleted from the table, but it is
not deleted from the underlying SQL data table.

I trap the RowDeleting event on the table and throw an exception if
the row should not be deleted (has children, etc.) This too works
fine. So the to me it appears the cm.RemoveAt line triggers the
RowDeleting event on the DataTable. My problem seems to be that the
actual DataRow DataRowState is not flagged as Deleted, so when I call
the Update method of the DataAdapter nothing is sent to the SQL
server.

If I call the AcceptChanges() method of the DataTable right after the
cm.RemoveAt() I get an exception Cannot delete row not in table.

I know I am missing something simple here. Any help would be
appreciated.

Thanks

Tim Holgerson
 
S

Sijin Joseph

You could post some code. Also have you tried deleting directly from the
datatable rather than using CurrencyManager?
 
K

Khalid Ashraf

Hi Tim,

i am not sure but you should not call "AcceptChanges" method of datatable
before calling the "Update" method of DataAdapter.



Sijin Joseph said:
You could post some code. Also have you tried deleting directly from the
datatable rather than using CurrencyManager?

--
Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph


Tim Holgerson said:
Hi,

I am new to C# and to the .NET data environment. I am having some
trouble understanding some of the events around deleting rows.

Here is my problem:

I delete the row from a bound datagrid using a currency mananger:
CurrencyManager cm = (CurrencyManager)
this.dataGridEng.BindingContext[this.oEngClassObj.EngObjDataSet.Tables["EngC
lasses"]];
if ( !(cm.List is DataViewManager) && ( cm.Count >= 0 ) )
{
try
{
cm.RemoveAt( this.dataGridEng.CurrentRowIndex);

}
catch (Exception ew)
{

this.evParentExceptHandler(this,ew);
}
}

this seems to work and the row is deleted from the table, but it is
not deleted from the underlying SQL data table.

I trap the RowDeleting event on the table and throw an exception if
the row should not be deleted (has children, etc.) This too works
fine. So the to me it appears the cm.RemoveAt line triggers the
RowDeleting event on the DataTable. My problem seems to be that the
actual DataRow DataRowState is not flagged as Deleted, so when I call
the Update method of the DataAdapter nothing is sent to the SQL
server.

If I call the AcceptChanges() method of the DataTable right after the
cm.RemoveAt() I get an exception Cannot delete row not in table.

I know I am missing something simple here. Any help would be
appreciated.

Thanks

Tim Holgerson
 

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