StrataFrame Forum

Using a DevExpress XtraGrid and Trying to Move Records Up or Down

http://forum.strataframe.net/Topic32668.aspx

By Buffie - 9/6/2014

I have a DevExpress XtraGrid filled with a Business Binding Source. I am trying to follow a Sample that allows user to move a record up or down by clicking a button.

But, the Sample uses a DataTable and not a BBS or BO so I am having trouble converting some of the code.

Dim view As GridView = gridView1
view.GridControl.Focus()
Dim index As Integer = view.FocusedRowHandle
If index <= 0 Then
Return
End If
Dim row1 As DataRow = view.GetDataRow(index)
Dim row2 As DataRow = view.GetDataRow(index - 1)
Dim val1 As Object = row1(OrderFieldName)
Dim val2 As Object = row2(OrderFieldName)
row1(OrderFieldName) = val2
row2(OrderFieldName) = val1
view.FocusedRowHandle = index - 1


What I am not sure how to convert is the lines that involve the DataRows.

I would appreciate any help I could get and if anyone has a Xtragrid allowing moving records I would appreciate any hints or advice.

TIA

By Edhy Rijo - 9/6/2014

Hi Terry,

I use the XtraGrid but never had the need to move records up/down nor I have not seen that sample you mention.

The gridview manage the records via its row handle index which it is used to locate the datarow record in the view.  The SF BBS should be able to internally detect the movement of the view row index and translate that to the SF BO.CurrentRowIndex, so I believe your sample code should work just fine.

Please post the link to the DevExpress sample and I will take a look at it.
By Buffie - 9/6/2014

I did not try the code because I assumed it would not work.

Here is the Link:

https://www.devexpress.com/Support/Center/Example/Details/E764
By Buffie - 9/6/2014

And by the way Edhy thanks for the help.
By Buffie - 9/7/2014

This seems to get the correct DataRows. I am not sure it is the correct way to do it?????

Dim row1 As DataRow = Me.ResultsPerfsChecksTimedEventBO1.CurrentRow()
Me.ResultsPerfsChecksTimedEventBO1.MovePrevious()
Dim row2 As DataRow = Me.ResultsPerfsChecksTimedEventBO1.CurrentRow()
Me.ResultsPerfsChecksTimedEventBO1.MoveNext()


I have not figured out how to get the 'Val' values....?

TIA.

By Buffie - 9/7/2014

Alright the Following Seems to Work but any ideas of a better way to do it or the Correct way to do it would be appreciated.

Dim row1 As DataRow = Me.ResultsPerfsChecksTimedEventBO1.CurrentRow()
Me.ResultsPerfsChecksTimedEventBO1.MovePrevious()
Dim row2 As DataRow = Me.ResultsPerfsChecksTimedEventBO1.CurrentRow()
Me.ResultsPerfsChecksTimedEventBO1.MoveNext()
Dim val1 As Object = row1("JudgesSheetPositionNumber")
Dim val2 As Object = row2("JudgesSheetPositionNumber")
row1("JudgesSheetPositionNumber") = val2
row2("JudgesSheetPositionNumber") = val1


Now on to the part of Mouse Drag and Drop.

 

By Edhy Rijo - 9/7/2014

Hi Terry,

I downloaded the demo yesterday but could not get back to it.

I will review it now with your code changes and get back in a few.
By Buffie - 9/7/2014

thanks Edhy
By Edhy Rijo - 9/7/2014

Hi Terry,

Basically this sample is expecting you have an Order field which they change in the datarow object and it is reflected in the grid because the grid is already ordered by that column named "Order" in their sample.

In your case that column is "JudgesSheetPositionNumber" so basically we would just do the same but at the BO level, assign the "JudgesSheetPositionNumber" value of the selected row to the value next or prior based on the Up/Down button but keep in mind that that will make the BO dirty and you will have to save it to keep the new position number by either asking the user or save it each time the position changes.

When using


' Assuming you are moving Down

Dim
 index As Integer = view.FocusedRowHandle
If index >= view.DataRowCount - 1 Then
      Return
End If

' Here create an instance of the BO with the record where the grid is focused
Dim
 boInGridView As New ResultsPerfsChecksTimedEventBO
boInGridView = TryCast(Me.GridView1.GetRow(index), ResultsPerfsChecksTimedEventBO1)
boInGridView.JudgesSheetPositionNumber = (boInGridView.JudgesSheetPositionNumber + 1)


Obviously you would need to add validations as to make sure when in 1st position you cannot move up or when at the end you cannot move down.

Try to make the functionality works moving Down and Up fist, then focus on make the Drag/Drop operations.

Please let me know if that works for you?

P.S.
Forgot t mention that this whole logic is based on the current sort order enforced in the grid by the sort column "JudgesSheetPositionNumber"  Without this sort in place you will not see the record being moved from one row to another in the grid.

gridView1.Columns("JudgesSheetPositionNumber").SortOrder = DevExpress.Data.ColumnSortOrder.Ascending         
gridView1.OptionsCustomization.AllowSort = False
By Buffie - 9/7/2014

Well my code Does Not Work if you try to move the Same Record more then one place.

I will have to look at your code Edhy and see what you have done.
By Terry Bottorff - 9/7/2014

I did set the SortOrder so I had that piece but I am confused. After I create the new BO based on the record that has Focus what am I to do with it?
I see it does have the JudgesSheetPositionNumber value in boInGridView.JudgesSheetPositionNumber and certainly when I add 1 to that it will tell me the position it is going but I don't know what to do from there? 

Also, the following line of code would not let me put BO1 on the end.

boInGridView = TryCast(Me.GridView1.GetRow(index), ResultsPerfsChecksTimedEventBO)

Yes Edhy, I agree I was planning on validating the move when in first position and last position.
By Edhy Rijo - 9/7/2014

Terry Bottorff (9/7/2014)

Also, the following line of code would not let me put BO1 on the end.

boInGridView = TryCast(Me.GridView1.GetRow(index), ResultsPerfsChecksTimedEventBO)

That is correct because when you Cast() or TryCast() you have to pass the class type as a parameter not an instance of a class which in this case would be the BO1, so my code is correct.
I know it can be confusing but if you look closely it is actually very simple. 

I have to go out now and will be back in 2 hours, if you want email me your phone and I will call you to make a remote session and see your project, this could get done in 10-20 minutes and would be easier to explain.
By Buffie - 9/7/2014

With Edhy's help and expertise I (we) were able to get the move to work in the DevExpress Grid.  Edhy is such a valuable resource....

If someone is interested in the code I will gladly post it.