Skip to content

Infragistics Community Forum / Desktop / Ultimate UI for Windows Forms / Set RowState to deleted in DataTable when deleting UltraGridRow

Set RowState to deleted in DataTable when deleting UltraGridRow

New Discussion
David Hooley
David Hooley asked on Oct 12, 2016 6:34 PM

To keep things simple, let’s say I have a DataSet that I declared at the top of the class (in vb.net), called testDT, and a DataSet called testDS.

In the BeforeRowsDeleted method, I’ve got my own MsgBox confirming that the user does wish to delete the relevant row. Before this MsgBox, I’m calling the following code; testDt = ugComm.DataSource, where ugComm is the UltraGrid that the row is deleted from, followed by testDS.Tables.Add(testDT). Next, if the user confirms they do want to delete the row, then the row is deleted, I then want update the DataTable so that it can still have the deleted row in it, but have the RowState as Deleted.

The reason I need to do this, is so that when updating the data, I can test

For Each dr as DataRow in testDT.Rows

If dr = datarowstate.deleted Then

   ' Execute Delete Query

Else

 'Upsert query

End If

 

Is there any way of doing this? Or, is there an alternative way that I’m missing?

Sign In to post a reply

Replies

  • 0
    Mike Saltzman
    Mike Saltzman answered on Oct 11, 2016 7:41 PM

    Hi,

    You seem to be going to a lot of trouble to re-create functionality that the DataSet and DataTable already provide. When you delete a row in the grid, the DataSet/DataTable already do what you want. They keep the deleted row and just mark it's RowState property as Deleted. The same is true of modified rows. This functionality is also supported by the DataAdapter class to allow you to easily update the data base with any changes made to the DataSet/DataTable in memory.

    Were you unaware of this functionality? Or is there some reason why you can't use the built-in support and you need to re-invent it yourself

    To answer your question… what you could do is cancel the delete operation by simply setting e.Cancel to true in BeforeRowsDeleted. Then you could do whatever you want – store the row in some other list or change a field in that row. The only problem with this is that the row will still display in the grid because it's not really deleted. To get around that, you could handle InitializeRow and check to see if your deleted flag is set on the row and if so, hide it. Of course if you have code that loops through the rows for any reason, those deleted rows will still be in there, so you will need to be aware of that and skip them when appropriate.

    • 0
      David Hooley
      David Hooley answered on Oct 12, 2016 9:14 AM

      Hi Mike,

      I have a DataTable set to being the datasource of the UltraGrid – It’s declared at the top of the class, then when loading the data, a new instance of it is created and the data is filled in.

      However, when running the following code at the start of my UPDATE statement, the number of rows returned is only the number of rows in the current grid, not including the deleted rows.

      tr = con.BeginTransaction()
      For Each dr As DataRow In commDt.Rows
      If dr.RowState = DataRowState.Deleted Then
      Try
      Dim Dc As New OleDbCommand

       

      • 0
        Mike Saltzman
        Mike Saltzman answered on Oct 12, 2016 2:08 PM

        Hi David, 

        How are you creating the DataSet? After it's created, you may need to call AcceptChanges to establish that as the unmodified version of the data. Otherwise, when you delete a row, that row's state was already Added, so the DataSet just removes the row and never "adds" it in the first place. 

        Also, I don't know if Deleted rows are included in the Rows collection. You should call the GetChanges method on the DataSet/DataTable. 

      • 0
        David Hooley
        David Hooley answered on Oct 12, 2016 4:50 PM

        Thanks Mike, it was just the way I was using and manipulating the datatable and set that was the issue, not the UltraGrid itself.

      • 0
        Mike Saltzman
        Mike Saltzman answered on Oct 12, 2016 6:34 PM

        Hi David, 

        Yeah, it can be a little tricky. When I tested this out before responding to your initial post, I called GetChanges and it always returned null. But then I figured out why. I was adding the rows and then deleting them and I never called AcceptChanges after adding them. So when you add a row and then delete that row, they consider it a no-op. 

        Anyway, I'm glad you got it sorted out. 

  • You must be logged in to reply to this topic.
Discussion created by
Favorites
Replies
Created On
Last Post
Discussion created by
David Hooley
Favorites
0
Replies
5
Created On
Oct 12, 2016
Last Post
9 years, 11 months ago

Suggested Discussions

Tags

Created by

Created on

Oct 12, 2016 6:34 PM

Last activity on

Feb 22, 2026 8:29 PM