Datagrid Delete

K

Kalvin

Please Help!
I have a datagrid bound to a datatable. the user should be able to
delete one or more rows. Then click a button to save the data, which
would then permanently delete the data from the database. However, as
soon as the user deletes a row, it is completed removed from the
datatable. From my understanding the row should have the rowstate
changed to deleted. How can I delete rows from a datagrid, and then
later update the rows to the database?

Thanks, Kalvin
 
D

DraguVaso

You need to work with a SqldataAdapter:
something like this:
' Create a new DataSet and fill its first DataTable.
dtsRules = New DataSet
SqlDataAdapter1.Fill(dtsRules, "Rules")

' Set up DataViews
dtvRules = New DataView(dtsRules.Tables("Rules"))

With dbgRules
.DataSource = dtvRules
End With


Than to do save your changes:
SqlDataAdapter1.Update(dtsRules.Tables(dtvRules.Table.TableName))



hope this helps? You can find an exemple in the VB.NET Resource Kit



Pieter
 
C

Cor Ligthert

Kalvin,

I assume you use a windowforms datagrid.

The most made mistake is that people are using the Remove which removes a
row completly from a datatable. The Delete set the rowstate to deleted while
the row stays in the dataset and will be removed with the next acceptchanges
(which is automaticly done by the dataadapter.update). Using the
acceptchanges (to early) is as well an often made mistake, there are very
few occasions that this "acceptchanges" would be used.

I hope this gives some ideas?

When not show some code?

Cor
 
K

Kalvin

Thank you very much for your help. I finally got it to do what I want.
This is how I understand it, please correct me if I am wrong.

You need to bind the datagrid to a dataview. Then, when you delete from
the grid, it deletes the row from the dataview and sets the rowstate of
the underlying table to deleted. When you are ready to update back to
the datasource start by filter the rowstate to deleted
(dsData.Tables(dvData.Table.TableName).DefaultView.RowStateFilter =
DataViewRowState.Deleted) and update the dataset.

Thank you again, I learned something great today.

Kalvin
 

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