Delete DataRow in DataTable ? [XPOST]

  • Thread starter Thread starter jez
  • Start date Start date
J

jez

I tried the following code, without success :

***
DataRow rowToDelete;
rowToDelete = productsTable.NewRow();

rowToDelete["customerID"] = listBoxCustomers.SelectedValue;
rowToDelete["productID"] =
dataGridProducts[dataGridProducts.CurrentCell.RowNumber,dataGridProducts.Cur
rentCell.ColumnNumber].ToString();
rowToDelete["product"] =
dataGridProducts[dataGridProducts.CurrentCell.RowNumber,(dataGridProducts.Cu
rrentCell.ColumnNumber)+1].ToString();

rowToDelete.Delete();
productsTable.AcceptChanges();
***

I don't get an error though.. I just want to delete the current row that is
selected in a DataGrid.

Am I doing anything wrong ? Is there another way that I could use to delete
rows ? (any way is welcome as long as it deletes the row...)

I tried to do a Rows.Find(PrimaryKeyValues)
but without success - I get an error saying I don't have any primary keys in
the table - even though I do. They're specified in a .xsd schema.

thanks, jez

[XPOST on microsoft.public.dotnet.framework.compactframework]
 
Jez,
The code that you have only deletes a row that is not a
the table, so nothing happens. You can iterate thru the
rows of the table to find the row that you want or you can
use the DataTable.Select() method to get the rows that
match the criteria.

Tu-Thach
 
I ended up using a Select Method and a Rows.Remove(foundRows) method to
delete the row. Thanks for the help !

Tu-Thach said:
Jez,
The code that you have only deletes a row that is not a
the table, so nothing happens. You can iterate thru the
rows of the table to find the row that you want or you can
use the DataTable.Select() method to get the rows that
match the criteria.

Tu-Thach
-----Original Message-----
I tried the following code, without success :

***
DataRow rowToDelete;
rowToDelete = productsTable.NewRow();

rowToDelete["customerID"] = listBoxCustomers.SelectedValue;
rowToDelete["productID"] =
dataGridProducts [dataGridProducts.CurrentCell.RowNumber,dataGridProducts.Cu
r
rentCell.ColumnNumber].ToString();
rowToDelete["product"] =
dataGridProducts[dataGridProducts.CurrentCell.RowNumber, (dataGridProducts.Cu
rrentCell.ColumnNumber)+1].ToString();

rowToDelete.Delete();
productsTable.AcceptChanges();
***

I don't get an error though.. I just want to delete the current row that is
selected in a DataGrid.

Am I doing anything wrong ? Is there another way that I could use to delete
rows ? (any way is welcome as long as it deletes the row...)

I tried to do a Rows.Find(PrimaryKeyValues)
but without success - I get an error saying I don't have any primary keys in
the table - even though I do. They're specified in a .xsd schema.

thanks, jez

[XPOST on microsoft.public.dotnet.framework.compactframework]



.
 
Hi jez,

jez said:
I ended up using a Select Method and a Rows.Remove(foundRows) method to
delete the row. Thanks for the help !

Just a note here: if you use Remove the row will disappear and it will *not*
be deleted in database when you use Update method.
Rather, use each row's Delete method which will mark row as Deleted and
Update will take appropriate actions.
 
Back
Top