You could firstly save the datasource(DataTable) , in selected change event of the gridview , you will get the row index , then you could move the row in datatable like :
DataRow row;
row = dtUp.row[index];
dtUp.Rows.RemoveAt[index];
dtUp.Rows.InsertAt[dr,index+1] //for down
dtUp.Rows.InsertAt[dr,index-1] //for up
Or you could use the inherited class to add a function for moving a row up or down within a DataTable :
Member
21 Points
151 Posts
Move grid rows Up and down using c#
Oct 09, 2016 09:33 AM|Sanfoor|LINK
i have a gridview, i need to move its records either up or down using a button outside the grid.
i.e after select the row, then click the button i can move it up or down
any help please?
Member
110 Points
37 Posts
Re: Move grid rows Up and down using c#
Oct 10, 2016 06:28 AM|mqingqign123|LINK
you need add a column eg. orderid
you can set it visible=false
in SQL order by orderid
eg. the table data list this
name country orderid
USA USA 1
China China 2
Japan Japan 3
when move china up,
you first need get china order (it is now is 2),and then get usa order (it is now 1)
swap their order id and rebind grid.
if you move china down, get china order (it is now 2) and the get japan order (it is now 3)
swap their order id and rebind grid.
at last, up btn click may like this.
in code abover, select top 1 max(orderid) from table where ordderid<"+currentOrderId meaning I first find orderid less currentOrderId
and the fetch the maximum
then use SQL swap theirs orderid。
when move down, the next is min(orderid) and
All-Star
18815 Points
3831 Posts
Re: Move grid rows Up and down using c#
Oct 10, 2016 07:49 AM|Nan Yu|LINK
Hi Sanfoor,
You could firstly save the datasource(DataTable) , in selected change event of the gridview , you will get the row index , then you could move the row in datatable like :
Or you could use the inherited class to add a function for moving a row up or down within a DataTable :
http://www.codeproject.com/Tips/312545/A-method-to-move-rows-within-a-DataTable
After that , rebind the gridview with DataTable. You could click here for discussion about same requirement as yours .
Best Regards,
Nan Yu