A
Alec MacLean
Hi,
I'm using a DataGridView in VS2005 on a Windows form. The DGV is bound to a table in a dataset.
I'm trying to allow the user to re-order the displayed sequence of records in the datagridview.
The dataset is loaded using the DAAB method of LoadDataSet from a SQL server.
The table contains an integer value that is the display position to set - column "RptOrder". This column is hidden from the user in the DGV. I'm editing this value programatically, then re-sorting the table so that the DGV stays visually synchronised with the desired display positions.
I have created a bit of code that should perform this (indeed it does work on the first pass), but it seems that the datatable values are not being persisted to the datatable, even though I am calling AcceptChanges. The code snippet below shows the row being moved down one position by swapping its "RptOrder" value with the item below it.
Taking one record as an example from a list of items, I try to move the first record down two positions, so that it should be displayed in the third row.
On the first pass through the code, the value for "RptOrder" appears to be modified correctly and the row is moved as desired - so far so good. However, on the subsequent call to the same method for the same item, the value of "RptOrder" is reset to its old value - the same as when the dataset was first loaded when opening the form.
Can anyone tell me why the dataset table values are getting restored to their original values after I'm editing them, despite my use of AcceptChanges?
<...>
'Calc new position. E.g. index = 0, RptOrder = 1.
NewPos = CInt(Me.dsCustomer.Tables("Reports").Rows(MovingRowIdx).Item("RptOrder").ToString) + 1
'Result: NewPos = 2.
'Update the position value of the row being moved.
Me.dsCustomer.Tables("Reports").Rows(MovingRowIdx).Item("RptOrder") = NewPos
'Update the row below to one position higher (selected row's original position).
Me.dsCustomer.Tables("Reports").Rows(MovingRowIdx + 1).Item("RptOrder") = NewPos - 1
<...>
'Re-sort the underlying table
Me.dtvReportSort.Sort = "RptOrder ASC"
'Remove any previous selection
Me.dgvReports.ClearSelection()
'Use the newpos to set new selection index in grid.
Me.dgvReports.Rows(NewPos - 1).Selected = True
<... end of sub>
Thanks for any help.
Al
I'm using a DataGridView in VS2005 on a Windows form. The DGV is bound to a table in a dataset.
I'm trying to allow the user to re-order the displayed sequence of records in the datagridview.
The dataset is loaded using the DAAB method of LoadDataSet from a SQL server.
The table contains an integer value that is the display position to set - column "RptOrder". This column is hidden from the user in the DGV. I'm editing this value programatically, then re-sorting the table so that the DGV stays visually synchronised with the desired display positions.
I have created a bit of code that should perform this (indeed it does work on the first pass), but it seems that the datatable values are not being persisted to the datatable, even though I am calling AcceptChanges. The code snippet below shows the row being moved down one position by swapping its "RptOrder" value with the item below it.
Taking one record as an example from a list of items, I try to move the first record down two positions, so that it should be displayed in the third row.
On the first pass through the code, the value for "RptOrder" appears to be modified correctly and the row is moved as desired - so far so good. However, on the subsequent call to the same method for the same item, the value of "RptOrder" is reset to its old value - the same as when the dataset was first loaded when opening the form.
Can anyone tell me why the dataset table values are getting restored to their original values after I'm editing them, despite my use of AcceptChanges?
<...>
'Calc new position. E.g. index = 0, RptOrder = 1.
NewPos = CInt(Me.dsCustomer.Tables("Reports").Rows(MovingRowIdx).Item("RptOrder").ToString) + 1
'Result: NewPos = 2.
'Update the position value of the row being moved.
Me.dsCustomer.Tables("Reports").Rows(MovingRowIdx).Item("RptOrder") = NewPos
'Update the row below to one position higher (selected row's original position).
Me.dsCustomer.Tables("Reports").Rows(MovingRowIdx + 1).Item("RptOrder") = NewPos - 1
<...>
'Re-sort the underlying table
Me.dtvReportSort.Sort = "RptOrder ASC"
'Remove any previous selection
Me.dgvReports.ClearSelection()
'Use the newpos to set new selection index in grid.
Me.dgvReports.Rows(NewPos - 1).Selected = True
<... end of sub>
Thanks for any help.
Al