0

I have an UltraGrid in my project, and I have an update function on the form to update the data in the database where the data is matching.

The data in the grid is being stored as a DataTable. If a row of existing data is deleted from the UltraGrid, I want to be able to set the RowState of that row in the DataTable to 'RowState.Deleted, so that in the Update function I can check theRowState`, and if it's a deleted row, then deleted it, otherwise, update the data.

How can I go about doing this? So far, I have the code below, but the count of rows being returned is 1 (or, the current number of rows in the grid) and not 2 (the number there was before I deleted one row).

How and where in the code, do I set the RowState of the deleted row to be RowState.Deleted? Is there an alternative way of doing it using the UltraGrid?

dsProducts.Tables.Add(commDt.Copy) -- commDt is the DataTable linked to the UltraGrid
tr = con.BeginTransaction(

  For Each dr As DataRow In dsProducts.Tables(0).Rows
    If dr.RowState = DataRowState.Deleted Then
     Try
David
  • 2,298
  • 6
  • 22
  • 56
  • You dont need to do any of that, Just delete the row (`DataRow.Delete()`) and the row state is set. If you use a DataAdapter, ***it*** will take the appropriate action for each row based on the rowstate: `Dim rows = myDA.Update(dsProducts.Tables(0))` – Ňɏssa Pøngjǣrdenlarp Oct 07 '16 at 16:19
  • @Plutonix I'm not programmatically setting the row to be deleted, that is handled automatically by the `UltraGrid`. By using a `DataAdapter`, do you mean for the actual update/delete query itself? Rather than on `OleDbCommand`? – David Oct 07 '16 at 16:22
  • This [Searching values via a datagridview](http://stackoverflow.com/a/33702351/1070452) shows how to configure and use a DataAdapter (`OleDBDataAdapter` in your case, I guess). They will "hold onto" Update, Delete etc commands and issue them via the Update method. – Ňɏssa Pøngjǣrdenlarp Oct 07 '16 at 16:24
  • When you remove a row from the `DataTable` you ***can't*** check the `RowState` property as it doesn't exist anymore. If you want, you could add another column of `Boolean` type and it can determine if record should be deleted or not when you need to update...Or another option is you would have to keep a reference to the `DataRow` and when removed you can check this object for the `RowState`... – Trevor Oct 07 '16 at 16:26
  • 1
    @Zaggler there is a difference between calling Delete and Remove on the row and in this scenario the row is being deleted and not removed, see this for more details: https://msdn.microsoft.com/en-us/library/03c7a3zb(v=vs.110).aspx – alhalama Oct 07 '16 at 20:58
  • @alhalama I know, `When you remove a row from the DataTable you can't check the RowState` I was saying remove not delete... – Trevor Oct 07 '16 at 21:02

2 Answers2

0

The grid will call mark the row as deleted (setting the RowState) and if you are using a TableAdapter then you can simply call Update on the table adapter passing the DataTable to the TableAdapter and it will do the updates in the database. For example:

ultraGrid1.UpdateData()
Me.dbRowsTableAdapter.Update(Me.testDataSet)

If you must manually handle updates and you want to get the deleted rows, you could call Select on the DataTable passing in DataViewRowState.Deleted for the third parameter.

alhalama
  • 3,218
  • 1
  • 15
  • 21
  • Hi, do you have any examples of how this would be done, for either method? – David Oct 11 '16 at 08:24
  • There is an example in this forum thread for using Update on a TableAdapter: http://www.infragistics.com/community/forums/t/57161.aspx – alhalama Oct 12 '16 at 20:31
0

Solved it.

  • Declare a DataTable at the top of the class

  • On the BeforeRowsUpdate method, insert a new row into the DataTable, which are the values of the row being deleted.

  • Optionally, include a confirmation box to ensure they wish to delete the row. If they cancel the deletion, remove the row from the DataTable.

  • This DataTable can then be used for deleting rows in the UPDATE query, whilst the grid DataSource can handle the UPSERTS

David
  • 2,298
  • 6
  • 22
  • 56