Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

'moving' rows w/in a DataGrid

Status
Not open for further replies.

Caobhin

Programmer
Dec 17, 2003
1
US
I'd like to build a UI that would allow one to 'manually' move rows w/in a DataGrid table. I'd have little arrows at the beginning of the row that would allow the user to move the row 'up' or 'down' w/in the table. This is not 'sorting' based on a field value.
Anyone ever seen such a thing?
 
I doubt it would be possible in a bound control, as the order of the rows is usually controlled by the SQL statement that fetches them from the database.

You could probably do something manually, with UPDATE statements behind the scenes -- When the user clicks the up button, look at the primary key of the row above it and swap values, then refresh the grid.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
To implement that you have to use DataView (dynamic view of a DataTable) to bind-data in the DataGrid control.
You can see a DataView object like a database view object but there is a big difference between DataView object and database view object, for example you cannot exclude columns from the original source table (DataTable) nor add new columns.
-obislavu-
 
In our project we are doing something similar to that, where user has the ability to move row/rows up and down and what we are doing reassigning the primary key values which is a sequential #. As an example if we have 10 rows and if the user selected 5th row and clicked up arrow, then we make the 5th row primary key to 4 and then 4th rows primary key to 5 and that does the trick in our case. So steps would be something like this...

Turn off the Sort on Primary key
Re-arrange the #s.
Turn on the Sort again.

-Kris
 
I understand you have a problem because the table has a primary key and you want to change it dynamically by swapping rows in a DataGrid object.
You achieve that by loading a database view object into a DataSet and use a DataView object to bind a DataGrid object.
- create a view object in the database which contains all the columns of the table in question and also other columns from other tables if needed to see in the DataGrid.
- Use System.Data.OleDb.OleDbDataAdapter or Sql* and DataSet object to load the content of the view object.
This is done by the Fill() method.
At this point the DataSet object contains only one DataTable which stores the records returned by the view object.
-bind the DataGrid object to the DataSet object and use a DataView object of the above DataTable object.
-Build the DataGridTableStyle
-implement handler to manage user editing in the in the grid
-implement handlers for the Row Up/Down buttons and there do the followings:
--locate DataRow that corresponds to the the current selected row in the grid
--swap the Seq# between this DataRow[idx] and DataRow[idx-1] or DataRow[idx+1] where 1<=idx< .Rows.Count()
-- refresh the DataGrid object to show the records always Seq# order
Note that the rows in the DataTable object DO NOT change the original (load) order but the Seq# could be 1,5,2,4,3...

-when saving the changes do the followings:
- connect to the source database
- drop the source table (the table that contains Seq # as primary key)
- Apply a Filter to retrieve the records from the DataSet in order of the Seq#
DataRow[] aRows = ds.Tables[0].Select(&quot;Seq# >= 0&quot;, &quot;Seq# ASC&quot;);
- iterate through aRows and Insert a row in the table source using ExecuteNoQuery();
Note that , there is nothing here when more than one users are working and they move up/down data in the grid.
They are working only in the memory on the DataSet and problems will appear when doing the Save.

-obislavu-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top