locked
Move grid rows Up and down using c# RRS feed

  • Question

  • User-1141190189 posted

    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?

     <header>
            <asp:Button ID="btn" runat="server" text="Up"/>
            <asp:Button ID="Button1" runat="server" text="Down"/>
        </header>
    <asp:GridView ID="GridView1" runat="server" CssClass="Grid" AutoGenerateColumns="false" OnSelectedIndexChange="row_select"
    EmptyDataText="No records has been added.">
    <Columns>
        <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="120" />
        <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="120" />
    </Columns>
    </asp:GridView>

    Sunday, October 9, 2016 9:33 AM

Answers

  • User1724605321 posted

    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 :

    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 :

    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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 10, 2016 7:49 AM

All replies

  • User2057738320 posted

    you need add a column eg. orderid 

     <asp:BoundField DataField="orderid" HeaderText="orderid" visible=false />

    you can set it visible=false

    in SQL  order by orderid

    select * from table order by orderid asc

    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.

    public void Up_Click()
    {
    //GET select row,using grid_indexSelected event remeber current select row orderid, eg. current row order id is 2

    object currentOrderId=2

    //get get usa order id

    sql=" select top 1 max(orderid) from table where ordderid<"+currentOrderId;


    cmd.Text=sql; object preOrderID=cmd.ExecuteScalar(); }

    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 

     select top 1  min(orderid)  from table where ordderid>"+currentOrderId

    Monday, October 10, 2016 6:28 AM
  • User1724605321 posted

    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 :

    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 :

    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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 10, 2016 7:49 AM