locked
Moving ALL cells in a row without remove RRS feed

  • Question

  • User2070972383 posted

    Hello, I'm new to C#, right now i'm trying to create a table with gridview, and move the data in the gridview up and down. However, i'm only able to move 2 cells only, i wanted to move all the cells inside the row up and down. I've tried google it. Most of the methods are remove and replace. Is there a method where i can move the rows up and down without remove and re-insert it.

       protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
               {
                    DataSource objdatasource = new DataSource();
                    gridview1.DataSource = objdatasource.MyDataTable();
                    gridview1.DataBind();
    
                }
            }
            protected void gridviewControl(object sender, GridViewCommandEventArgs e)
            {
                int rowid = Convert.ToInt32(e.CommandArgument);
    
                if (e.CommandName == "Up")
                {
                    if (rowid > 0)      
                        Swap(rowid, "UP");
                }
    
                if (e.CommandName == "Down")
                {
                    if (rowid != gridview1.Rows.Count - 1)
                        Swap(rowid, "DOWN");
                }
            }

      private void Swap(int rowid, string option)
            {
               
    
                if (option == "UP")
                {
    
    
                    for (int i = 0; i <= 1; i++)
                    {
                        
                        
                        string temp = gridview1.Rows[rowid - 1].Cells[i].Text;
                        gridview1.Rows[rowid - 1].Cells[i].Text = gridview1.Rows[i].Cells[i].Text;
                        gridview1.Rows[rowid].Cells[i].Text = temp;
                    }
                }
                else
                {
                    for (int i = 0; i <= 1; i++)
                    {
                        string temp = gridview1.Rows[rowid].Cells[i].Text;
                        gridview1.Rows[rowid].Cells[i].Text = gridview1.Rows[rowid + 1].Cells[i].Text;
                        gridview1.Rows[rowid + 1].Cells[i].Text = temp;
                    }
                }

    i suppose its obvious why i cant move the whole cell in a row up and down because i have a cell[i] over there. so any suggestion on how i should modify the code?

    Friday, August 11, 2017 4:47 AM

Answers

  • User-1838255255 posted

    Hi steven7436,

    According to your description, I checked your code again, I know your method is different from the tutorial that I post.

    Your method is replace the first row and the second row cell text, and don't save these change in database.

    My tutorial is change the index of the row in database then rebind the database to GridView.

    You say you only change the cell value in column1 and column2, the cell value not change in column3 and column4.

    You need modify your for loop like this(change i to 3):

    Sample Code:

    Modify your for loop:
    for (int i = 0; i <= 1; i++)
    {
          //your replace code 
    }
    To:
    for (int i = 0; i <= 3; i++)
    {
         //your replace code 
    }
    

    Best Regards,

    Eric Du

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 16, 2017 8:34 AM

All replies

  • User-1838255255 posted

    Hi steven7436,

    According to your description, I think you could realize it like this:

    1. when you click it, it will get this row index. 

    2. through judge which button you click, add or reduce "1" to this index.

    3. update database then rebind the data.

    Here is a complete tutorial about how to realize it, please check:

    Moving ASP.Net Gridview Rows Up and Down with Arrow Button click:

    https://www.aspsnippets.com/Articles/Moving-ASPNet-Gridview-Rows-Up-and-Down-with-Arrow-Button-click.aspx 

    Best Regards,

    Eric Du

    Saturday, August 12, 2017 3:50 AM
  • User2070972383 posted

    Thank you for replying Eric.

    Yes, My coding is mostly based on that. However, My table contain 4 column. i managed to move the cell data in column 1 and 2 (from selected row). Cell data in Column 3 and 4 remain unchanged. I'm able to move the row, but the cell data in column 3 and 4 i couldn't move it.

    http://imgur.com/a/JaV2l

    I've attached a link above just in case.

    Monday, August 14, 2017 1:52 AM
  • User-1838255255 posted

    Hi steven7436,

    According to your description, I checked your code again, I know your method is different from the tutorial that I post.

    Your method is replace the first row and the second row cell text, and don't save these change in database.

    My tutorial is change the index of the row in database then rebind the database to GridView.

    You say you only change the cell value in column1 and column2, the cell value not change in column3 and column4.

    You need modify your for loop like this(change i to 3):

    Sample Code:

    Modify your for loop:
    for (int i = 0; i <= 1; i++)
    {
          //your replace code 
    }
    To:
    for (int i = 0; i <= 3; i++)
    {
         //your replace code 
    }
    

    Best Regards,

    Eric Du

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 16, 2017 8:34 AM