Odpovědět C# datagridview MultiRows Move with Up/Down Buttons?

  • 13. března 2012 6:38
     
     

    Hi everybody,

    I have  a form application in c# with a datagridview and two buttons UP and DOWN.  What i can't is to move selected rows when i clicked the buttons.

    This is my first topic in the form. Thanks you for your helps.

    Regards.


    • Přesunutý CoolDadTxMVP 13. března 2012 14:38 Winforms related (From:Visual C# General)
    • Upravený Mutekin 15. března 2012 13:16 Update
    •  

Všechny reakce

  • 13. března 2012 6:51
     
     

    Hi Mutekin

    There are several possible ways to bell this cat.

    1. If you are using DataTable as the Source for your GridView, then On click of Up button you need to Remove DataRow from selected index

    and insert that row at index -1. For Click of Down do the other way (remove from current index and add at index + 1).

    2. If you add the rows manually (using a For/While loop statement), then you will have to implement the above Row remove/add logic manually.


    .NET Maniac -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

  • 13. března 2012 12:10
     
      Obsahuje kód

    Take this for Up and Down

                if (dataGridView1.CurrentRow == null) return;
                if (dataGridView1.CurrentRow.Index + 1 <= dataGridView1.Rows.Count -1)
                {
                    dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.CurrentRow.Index + 1].Cells[0];
                    dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Selected = true;
                } 

                if (dataGridView1.CurrentRow == null) return;
                if (dataGridView1.CurrentRow.Index - 1 >= 0)
                {
    
                    dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.CurrentRow.Index - 1].Cells[0];
                    dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Selected = true;
                }


  • 13. března 2012 13:15
     
     

    Hi Mr Prateek,

    I havent used a datatable just fill the cells of the rows and then need to move it.

    I tried what you said but i coulnd't. 

  • 13. března 2012 13:42
     
      Obsahuje kód

    Well, i fill the gridview like this:

            private void LoadData()
            {
    
                DataTable dt = new DataTable();
                dt.Columns.Add("ID", typeof(int));
                dt.Columns.Add("Text");
    
                DataRow dr;
    
                dr = dt.NewRow();
                dr["ID"] = 1;
                dr["Text"] = "blabla1";
                dt.Rows.Add(dr);
    
                dr = dt.NewRow();
                dr["ID"] = 2;
                dr["Text"] = "blabla2";
                dt.Rows.Add(dr);
    
                dataGridView1.DataSource = dt;
    
            }

    And then i have one button with that code:

                if (dataGridView1.CurrentRow == null) return;
                if (dataGridView1.CurrentRow.Index + 1 <= dataGridView1.Rows.Count -1)
                {
                    dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.CurrentRow.Index + 1].Cells[0];
                    dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Selected = true;
                }   

  • 13. března 2012 14:44
     
     

    Hi Mr Prateek,

    I havent used a datatable just fill the cells of the rows and then need to move it.

    I tried what you said but i coulnd't. 

    Hi Mutekin

    If you can post the code snippet where you Populate/Add Rows to your Grid, it will be helpful to reply.



    .NET Maniac -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

  • 14. března 2012 17:27
     
     

    Hi,

    I uploaded my form application and add the link here:

    http://www.2shared.com/file/7Lk-4h29/WindowsFormsApplication1.html

    Thanks again.

    Mustafa




  • 15. března 2012 2:53
    Moderátor
     
     
    Hi Mutekin,
    I can’t download the project form the link, could you please upload it to SkyDrive?
    Best Regards,

    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us


  • 15. března 2012 5:58
     
      Obsahuje kód

    I added two Buttons (Up and Down) to your Form1 and used the following code. Like this i can move the selected row:

    Button UP:

        private void buttonUp_Click(object sender, EventArgs e)
        {
     
            if (dataGridView1.CurrentRow == nullreturn;
            if (dataGridView1.CurrentRow.Index + 1 <= dataGridView1.Rows.Count - 1)
            {
                dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.CurrentRow.Index + 1].Cells[0];
                dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Selected = true;
            } 
     
        }

    Button Down:

        private void buttonDown_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow == nullreturn;
            if (dataGridView1.CurrentRow.Index - 1 >= 0)
            {
     
                dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.CurrentRow.Index - 1].Cells[0];
                dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Selected = true;
            }
        }



  • 15. března 2012 6:10
     
      Obsahuje kód

    In Detail:

    Add this two buttons in your constructor:

            System.Windows.Forms.Button btnUp = new System.Windows.Forms.Button();
            this.Controls.Add(btnUp);
            btnUp.Text = "Up";
            btnUp.Location = new System.Drawing.Point(765, 115);
            btnUp.Click += new EventHandler(buttonUp_Click);
     
            System.Windows.Forms.Button btnDown = new System.Windows.Forms.Button();
            this.Controls.Add(btnDown);
            btnDown.Text = "Down";
            btnDown.Location = new System.Drawing.Point(765, 144);
            btnDown.Click += new EventHandler(buttonDown_Click);

    And this two functions:

        private void buttonUp_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow == nullreturn;
            if (dataGridView1.CurrentRow.Index - 1 >= 0)
            {
     
                dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.CurrentRow.Index - 1].Cells[0];
                dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Selected = true;
            }
        }
     
        private void buttonDown_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow == nullreturn;
            if (dataGridView1.CurrentRow.Index + 1 <= dataGridView1.Rows.Count - 1)
            {
                dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.CurrentRow.Index + 1].Cells[0];
                dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Selected = true;
            } 
        }

  • 15. března 2012 6:52
     
     

    Mr Padma,

    I added all your instructs to my code but when click the buttons only selected row's highligt moves not the row. By the way i need to move "selected" rows up and down,  may i move two rows at the same time.

    Thanks for your sparing time...


    • Upravený Mutekin 15. března 2012 6:53 update
    •  
  • 15. března 2012 7:20
     
      Obsahuje kód

    Add two new buttons for moving in your constructor:

            System.Windows.Forms.Button btnRowUp = new System.Windows.Forms.Button();
            this.Controls.Add(btnRowUp);
            btnRowUp.Text = "Row Up";
            btnRowUp.Location = new System.Drawing.Point(765, 180);
            btnRowUp.Click += new EventHandler(buttonRowUp_Click);
     
            System.Windows.Forms.Button btnRowDown = new System.Windows.Forms.Button();
            this.Controls.Add(btnRowDown);
            btnRowDown.Text = "Row Down";
            btnRowDown.Location = new System.Drawing.Point(765, 210);
            btnRowDown.Click += new EventHandler(buttonRowDown_Click);

    And now the two functions for moving the rows up and down:

        private void buttonRowUp_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                DataGridViewRow row = dataGridView1.SelectedRows[0];
                int selRowIndex = row.Index;
     
                if (selRowIndex == 0) { return; }
     
                dataGridView1.Rows.Remove(row);
                dataGridView1.Rows.Insert(selRowIndex - 1, row);
                dataGridView1.Rows[selRowIndex - 1].Selected = true;
                dataGridView1.CurrentCell = dataGridView1.Rows[selRowIndex - 1].Cells[0];
            }
        }
     
        private void buttonRowDown_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                DataGridViewRow row = dataGridView1.SelectedRows[0];
                int selRowIndex = row.Index;
     
                if ((selRowIndex + 1) == dataGridView1.Rows.Count - 1) { return; }
     
                dataGridView1.Rows.Remove(row);
                dataGridView1.Rows.Insert(selRowIndex + 1, row);
                dataGridView1.Rows[selRowIndex + 1].Selected = true;
                dataGridView1.CurrentCell = dataGridView1.Rows[selRowIndex + 1].Cells[0];
            }
        }

  • 15. března 2012 8:36
     
     

    Now i can move a single row up and down Mr Padma. But when i select two or three rows only the last row move?

  • 15. března 2012 9:43
     
      Obsahuje kód

    Check this:

        private void buttonMultiRowUp_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                DataGridViewSelectedRowCollection sr = dataGridView1.SelectedRows;
                for (int i = 0; i <= sr.Count - 1; i++)
                {
                    int selRowIndex = sr[i].Index;
                    if (selRowIndex > 0)
                    {
                        dataGridView1.Rows.Remove(sr[i]);
                        dataGridView1.Rows.Insert(selRowIndex - 1, sr[i]);
                        dataGridView1.Rows[selRowIndex - 1].Selected = true;
                    }
                }
            }
        }
     
        private void buttonMultiRowDown_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                DataGridViewSelectedRowCollection sr = dataGridView1.SelectedRows;
                for (int i = 0; i <= sr.Count - 1; i++)
                {
                    int selRowIndex = sr[i].Index;
                    if ((selRowIndex < dataGridView1.Rows.Count - 1) && (!(selRowIndex == dataGridView1.Rows.Count - 2)))
                    {
                        dataGridView1.Rows.Remove(sr[i]);
                        if ((selRowIndex + 1) == dataGridView1.Rows.Count)
                        {
                            dataGridView1.Rows.Add();
     
                        }
                        dataGridView1.Rows.Insert(selRowIndex + 1, sr[i]);
                        dataGridView1.Rows[selRowIndex + 1].Selected = true;
                    }
                }
            }
        }


  • 15. března 2012 11:55
     
      Obsahuje kód

    Well,

    i added two new buttons in the constructor:

            System.Windows.Forms.Button btnMultiRowUp = new System.Windows.Forms.Button();
            this.Controls.Add(btnMultiRowUp);
            btnMultiRowUp.Text = "Multi Row Up";
            btnMultiRowUp.Width = 100;
            btnMultiRowUp.Location = new System.Drawing.Point(765, 250);
            btnMultiRowUp.Click += new EventHandler(buttonMultiRowUp_Click);
     
            System.Windows.Forms.Button btnMultiRowDown = new System.Windows.Forms.Button();
            this.Controls.Add(btnMultiRowDown);
            btnMultiRowDown.Text = "Multi Row Down";
            btnMultiRowDown.Width = 100;
            btnMultiRowDown.Location = new System.Drawing.Point(765, 280);
            btnMultiRowDown.Click += new EventHandler(buttonMultiRowDown_Click);

    And then the two new functions:

        private void buttonMultiRowUp_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                DataGridViewSelectedRowCollection sr = dataGridView1.SelectedRows;
                for (int i = 0; i <= sr.Count - 1; i++)
                {
                    int selRowIndex = sr[i].Index;
                    if (selRowIndex > 0)
                    {
                        dataGridView1.Rows.Remove(sr[i]);
                        dataGridView1.Rows.Insert(selRowIndex - 1, sr[i]);
                        dataGridView1.Rows[selRowIndex - 1].Selected = true;
                    }
                }
            }
        }
     
        private void buttonMultiRowDown_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                DataGridViewSelectedRowCollection sr = dataGridView1.SelectedRows;
                for (int i = 0; i <= sr.Count - 1; i++)
                {
                    int selRowIndex = sr[i].Index;
                    if ((selRowIndex < dataGridView1.Rows.Count - 1) && (!(selRowIndex == dataGridView1.Rows.Count - 2)))
                    {
                        dataGridView1.Rows.Remove(sr[i]);
                        if ((selRowIndex + 1) == dataGridView1.Rows.Count)
                        {
                            dataGridView1.Rows.Add();
     
                        }
                        dataGridView1.Rows.Insert(selRowIndex + 1, sr[i]);
                        dataGridView1.Rows[selRowIndex + 1].Selected = true;
                    }
                }
            }
        }

  • 15. března 2012 12:12
     
     

    Two little comments:

     1- When i select one row and move up/down it works but after that two selected rows are highlihted. Infact there must be one selected rows. So i can clickthe buton again and again.

    2-When i select two rows and move rows with up/down buttons by"twice cilicking" it works and selection is ok. But to move again i must clicked twice again. Can it be with only single click when i selected multirows.

    After that it will be unique c# datagridview appliaction i have ever seen on the internet:)

    Thanks.


    • Upravený Mutekin 15. března 2012 12:15 update
    •  
  • 15. března 2012 13:05
     
     Navržená odpověď Obsahuje kód

    I fixed it!

    Replace this two functions:

        private void buttonMultiRowDown_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                List<DataGridViewRow> SelectedRows = new List<DataGridViewRow>();
                foreach (DataGridViewRow dgvr in dataGridView1.SelectedRows)
                {
                    SelectedRows.Add(dgvr);
                }
     
                SelectedRows.Sort(DataGridViewRowIndexCompare);
     
                for (int i = SelectedRows.Count - 1; i >= 0; i--)
                {
                    int selRowIndex = SelectedRows[i].Index;
                    if ((selRowIndex < dataGridView1.Rows.Count - 1) && (!(selRowIndex == dataGridView1.Rows.Count - 2)))
                    {
                        dataGridView1.Rows.Remove(SelectedRows[i]);
                        if ((selRowIndex + 1) == dataGridView1.Rows.Count)
                        {
                            dataGridView1.Rows.Add();
     
                        }
                        dataGridView1.Rows.Insert(selRowIndex + 1, SelectedRows[i]);
                        dataGridView1.Rows[selRowIndex + 1].Selected = true;
                    }
     
                }
     
            }
        }
     
        private void buttonMultiRowUp_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
     
                List<DataGridViewRow> SelectedRows = new List<DataGridViewRow>();
                foreach (DataGridViewRow dgvr in dataGridView1.SelectedRows)
                {
                    SelectedRows.Add(dgvr);
                }
     
                SelectedRows.Sort(DataGridViewRowIndexCompare);
     
                for (int i = 0; i <= SelectedRows.Count - 1; i++)
                {
                    int selRowIndex = SelectedRows[i].Index;
                    if (selRowIndex > 0)
                    {
                        dataGridView1.Rows.Remove(SelectedRows[i]);
                        dataGridView1.Rows.Insert(selRowIndex - 1, SelectedRows[i]);
                        dataGridView1.Rows[selRowIndex - 1].Selected = true;
                    }
     
                }
     
                
            }
        }

    And add this function for sorting the SelectedRowList:

        private static int DataGridViewRowIndexCompare(DataGridViewRow x, DataGridViewRow y)
        {
            if (x == null)
            {
                if (y == null)
                {
                    // If x is null and y is null, they're
                    // equal. 
                    return 0;
                }
                else
                {
                    // If x is null and y is not null, y
                    // is greater. 
                    return -1;
                }
            }
            else
            {
                // If x is not null...
                //
                if (y == null)
                // ...and y is null, x is greater.
                {
                    return 1;
                }
                else
                {
                    // ...and y is not null, compare the 
                    // lengths of the two strings.
                    //
                    int retval = x.Index.CompareTo(y.Index);
     
                    if (retval != 0)
                    {
                        // If the strings are not of equal length,
                        // the longer string is greater.
                        //
                        return retval;
                    }
                    else
                    {
                        // If the strings are of equal length,
                        // sort them with ordinary string comparison.
                        //
                        return x.Index.CompareTo(y.Index);
                    }
                }
            }
        }

    • Navržen jako odpověď Padma Lahore 15. března 2012 13:13
    • Označen jako odpověď Mutekin 15. března 2012 13:16
    • Zrušeno označení jako odpověď Mutekin 16. března 2012 6:14
    •  
  • 15. března 2012 13:27
     
     

     Mr Padma sorry, my first comment still exist in the form.

    1- When i select one row and move up/down it works but after that two selected rows are highlihted. Infact there must be one selected rows. So i can clickthe buton again and again.

    • Upravený Mutekin 15. března 2012 13:34 Update
    •  
  • 15. března 2012 13:34
     
     

    Thank you Mutekin, you're welcome!

  • 15. března 2012 13:54
     
     

    "You select one row in the row header and then you press move down, move down, move down, move down and this is OK."

    I select one row in the row header and press move down. That row moves down but in datagridview 2 rows are in selected status. So if i press again (second press) 2 rows move down instead of one. I mean after i select one row i cant continuosly move it by pressing buton. The problem is selected row must be one after first click, but it becomes 2. 

  • 15. března 2012 13:54
     
      Obsahuje kód

    Replace this two functions:

        private void buttonMultiRowDown_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
     
                List<DataGridViewRow> SelectedRows = new List<DataGridViewRow>();
                foreach (DataGridViewRow dgvr in dataGridView1.SelectedRows)
                {
                    SelectedRows.Add(dgvr);
                }
     
                SelectedRows.Sort(DataGridViewRowIndexCompare);
     
                for (int i = SelectedRows.Count - 1; i >= 0; i--)
                {
                    int selRowIndex = SelectedRows[i].Index;
                    if ((selRowIndex < dataGridView1.Rows.Count - 1) && (!(selRowIndex == dataGridView1.Rows.Count - 2)))
                    {
     
                        dataGridView1.Rows.Remove(SelectedRows[i]);
                        if ((selRowIndex + 1) == dataGridView1.Rows.Count)
                        {
                            dataGridView1.Rows.Add();
     
                        }
                        dataGridView1.Rows[selRowIndex].Selected = false;
                        dataGridView1.Rows.Insert(selRowIndex + 1, SelectedRows[i]);
                        dataGridView1.Rows[selRowIndex + 1].Selected = true;
                        
                    }
     
                }
     
            }
        }
     
        private void buttonMultiRowUp_Click(object sender, EventArgs e)
        {
     
            if (dataGridView1.SelectedRows.Count > 0)
            {
     
                List<DataGridViewRow> SelectedRows = new List<DataGridViewRow>();
                foreach (DataGridViewRow dgvr in dataGridView1.SelectedRows)
                {
                    SelectedRows.Add(dgvr);
                }
     
                SelectedRows.Sort(DataGridViewRowIndexCompare);
     
                for (int i = 0; i <= SelectedRows.Count - 1; i++)
                {
                    int selRowIndex = SelectedRows[i].Index;
                    if (selRowIndex > 0)
                    {
                        dataGridView1.Rows[selRowIndex].Selected = false;
                        dataGridView1.Rows.Remove(SelectedRows[i]);
                        dataGridView1.Rows.Insert(selRowIndex - 1, SelectedRows[i]);
                        dataGridView1.Rows[selRowIndex - 1].Selected = true;
                    }
     
                }
                //dataGridView1.CurrentCell = dataGridView1.Rows[SelectedRows[SelectedRows.Count - 1].Index].Cells[0];
     
                
            }
        }

  • 15. března 2012 14:12
     
     
    Mr Padma after your last fix multirows actions are Ok.
    Only problem i can see is select a row and press Upbtn once, 2 selected rows are seen.
  • 15. března 2012 14:16
     
     

    It happens when you move to/over "focused" row (dataGridView1.CurrentCell)

    Look at the arrow on the left side. I'm looking for a solution.

  • 15. března 2012 14:18
     
      Obsahuje kód

    Next fix.

    Replace the two functions:

        private void buttonMultiRowDown_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
     
                List<DataGridViewRow> SelectedRows = new List<DataGridViewRow>();
                foreach (DataGridViewRow dgvr in dataGridView1.SelectedRows)
                {
                    SelectedRows.Add(dgvr);
                }
     
                SelectedRows.Sort(DataGridViewRowIndexCompare);
     
                for (int i = SelectedRows.Count - 1; i >= 0; i--)
                {
                    int selRowIndex = SelectedRows[i].Index;
                    if ((selRowIndex <= dataGridView1.Rows.Count - 1) && (!(selRowIndex == dataGridView1.Rows.Count - 1)))
                    {
     
                        dataGridView1.Rows.Remove(SelectedRows[i]);
                        if ((selRowIndex + 1) == dataGridView1.Rows.Count)
                        {
                            dataGridView1.Rows.Add();
     
                        }
                        dataGridView1.Rows[selRowIndex].Selected = false;
                        dataGridView1.Rows.Insert(selRowIndex + 1, SelectedRows[i]);
                        dataGridView1.Rows[selRowIndex + 1].Selected = true;
                        
                    }
     
                }
     
            }
        }
     
        private void buttonMultiRowUp_Click(object sender, EventArgs e)
        {
     
            if (dataGridView1.SelectedRows.Count > 0)
            {
     
                List<DataGridViewRow> SelectedRows = new List<DataGridViewRow>();
                foreach (DataGridViewRow dgvr in dataGridView1.SelectedRows)
                {
                    SelectedRows.Add(dgvr);
                }
     
                SelectedRows.Sort(DataGridViewRowIndexCompare);
     
                for (int i = 0; i <= SelectedRows.Count - 1; i++)
                {
                    int selRowIndex = SelectedRows[i].Index;
                    if (selRowIndex > 0)
                    {
                        dataGridView1.Rows[selRowIndex].Selected = false;
                        dataGridView1.Rows.Remove(SelectedRows[i]);
                        dataGridView1.Rows.Insert(selRowIndex - 1, SelectedRows[i]);
                        dataGridView1.Rows[selRowIndex - 1].Selected = true;
                        
                    }
                }
     
                if (dataGridView1.SelectedRows.Count == 1)
                {
                    dataGridView1.CurrentCell = dataGridView1.Rows[SelectedRows[0].Index].Cells[0];
                }
     
              
                
            }
        }

    And this function for sorting the list of SelectedRows:

        private static int DataGridViewRowIndexCompare(DataGridViewRow x, DataGridViewRow y)
        {
            if (x == null)
            {
                if (y == null)
                {
                    // If x is null and y is null, they're
                    // equal. 
                    return 0;
                }
                else
                {
                    // If x is null and y is not null, y
                    // is greater. 
                    return -1;
                }
            }
            else
            {
                // If x is not null...
                //
                if (y == null)
                // ...and y is null, x is greater.
                {
                    return 1;
                }
                else
                {
                    // ...and y is not null, compare the 
                    // lengths of the two strings.
                    //
                    int retval = x.Index.CompareTo(y.Index);
     
                    if (retval != 0)
                    {
                        // If the strings are not of equal length,
                        // the longer string is greater.
                        //
                        return retval;
                    }
                    else
                    {
                        // If the strings are of equal length,
                        // sort them with ordinary string comparison.
                        //
                        return x.Index.CompareTo(y.Index);
                    }
                }
            }
        }

  • 15. března 2012 14:27
     
     

    Still same problem. At only single row selected and Up btn pressed.

    "Look at the arrow on the left side. I'm looking for a solution." Pls do not hurry.

    I got your "Have you tried this code above"msg by email, but i cant response as i cant see you email.

    Yes i tried but same thing happened again.

    • Upravený Mutekin 15. března 2012 14:39 Update
    •  
  • 15. března 2012 14:39
     
      Obsahuje kód

    Try this:

        private void buttonMultiRowDown_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count == 0) return;
     
            if (dataGridView1.SelectedRows.Count == 0)
            {
                if (dataGridView1.CurrentCell.RowIndex > -1)
                {
                    dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Selected = true; 
                }
                else
                {
                    return;
                }    
            }
     
            List<DataGridViewRow> SelectedRows = new List<DataGridViewRow>();
            foreach (DataGridViewRow dgvr in dataGridView1.SelectedRows)
            {
                SelectedRows.Add(dgvr);
            }
     
            SelectedRows.Sort(DataGridViewRowIndexCompare);
     
            for (int i = SelectedRows.Count - 1; i >= 0; i--)
            {
                int selRowIndex = SelectedRows[i].Index;
                if ((selRowIndex <= dataGridView1.Rows.Count - 1) && (!(selRowIndex == dataGridView1.Rows.Count - 1)))
                {
     
                    dataGridView1.Rows.Remove(SelectedRows[i]);
                    if ((selRowIndex + 1) == dataGridView1.Rows.Count)
                    {
                        dataGridView1.Rows.Add();
     
                    }
                    dataGridView1.Rows[selRowIndex].Selected = false;
                    dataGridView1.Rows.Insert(selRowIndex + 1, SelectedRows[i]);
                    dataGridView1.Rows[selRowIndex + 1].Selected = true;
                        
                }
     
            }
     
        }
     
        private void buttonMultiRowUp_Click(object sender, EventArgs e)
        {
     
            if (dataGridView1.Rows.Count == 0) return;
     
            if (dataGridView1.SelectedRows.Count == 0)
            {
                if (dataGridView1.CurrentCell.RowIndex > -1)
                {
                    dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Selected = true; 
                }
                else
                {
                    return;
                }    
            }
     
            List<DataGridViewRow> SelectedRows = new List<DataGridViewRow>();
            foreach (DataGridViewRow dgvr in dataGridView1.SelectedRows)
            {
                SelectedRows.Add(dgvr);
            }
     
            SelectedRows.Sort(DataGridViewRowIndexCompare);
     
            for (int i = 0; i <= SelectedRows.Count - 1; i++)
            {
                int selRowIndex = SelectedRows[i].Index;
                if (selRowIndex > 0)
                {
                    dataGridView1.Rows[selRowIndex].Selected = false;
                    dataGridView1.Rows.Remove(SelectedRows[i]);
                    dataGridView1.Rows.Insert(selRowIndex - 1, SelectedRows[i]);
                    dataGridView1.Rows[selRowIndex - 1].Selected = true;
                        
                }
            }
     
            if (dataGridView1.SelectedRows.Count == 1)
            {
                dataGridView1.CurrentCell = dataGridView1.Rows[SelectedRows[0].Index].Cells[0];
            }
        }
     
        private static int DataGridViewRowIndexCompare(DataGridViewRow x, DataGridViewRow y)
        {
            if (x == null)
            {
                if (y == null)
                {
                    // If x is null and y is null, they're
                    // equal. 
                    return 0;
                }
                else
                {
                    // If x is null and y is not null, y
                    // is greater. 
                    return -1;
                }
            }
            else
            {
                // If x is not null...
                //
                if (y == null)
                // ...and y is null, x is greater.
                {
                    return 1;
                }
                else
                {
                    // ...and y is not null, compare the 
                    // lengths of the two strings.
                    //
                    int retval = x.Index.CompareTo(y.Index);
     
                    if (retval != 0)
                    {
                        // If the strings are not of equal length,
                        // the longer string is greater.
                        //
                        return retval;
                    }
                    else
                    {
                        // If the strings are of equal length,
                        // sort them with ordinary string comparison.
                        //
                        return x.Index.CompareTo(y.Index);
                    }
                }
            }
        }

  • 15. března 2012 14:43
     
     

    Same problem :(

  • 15. března 2012 14:44
     
     

    I can't replicate it. :(

    Please tell me what you are doing step by step.

    1. Choose file

    2. Write Description

    3. Click here

    4. Click there

    .... and so on

  • 15. března 2012 14:51
     
     

    I see.

    1-I run the application (F5)

    2-Click a row's cell (as Rowheadervisible=False) Let's say row_2

    3-Click MultiRowUp btn

    4-row_2 move up and selected; but at the same time row_3 are selected


    • Upravený Mutekin 15. března 2012 14:53
    •  
  • 15. března 2012 14:56
     
     

    Thanks for the fast answer but i simulated your steps 1-4 and your described problem is not showing up.

    Can you upload your current project a second time?

  • 15. března 2012 15:03
     
     

    Sorry Mutekin, i will not publish my email adress here.

    But tomorrow we will fix this little bug.

  • 15. března 2012 15:10
     
      Obsahuje kód

    Can you test the following in your constructor:

    dataGridView1.RowHeadersVisible = true;
    

    As i see the grid is acting different when i set the "RowHeadersVisible = false", so please check if your project is acting different when showing the rowheaders


  • 15. března 2012 15:31
     
     

    another question:

    You use everytime the "Multi Row Up" and "Multi Row Down" buttons ?

    The single "up" and "down" button are obsolete.

    Singlerow and multirow moving must function with the "Multi Row Up/Down" buttons

  • 16. března 2012 6:09
     
     Odpovědět Obsahuje kód

    Another fix, try this:

        private void buttonMultiRowDown_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count == 0) return;
     
            if (dataGridView1.SelectedRows.Count == 0)
            {
                if (dataGridView1.CurrentCell.RowIndex > -1)
                {
                    dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Selected = true;
                }
                else
                {
                    return;
                }
            }
     
            List<DataGridViewRow> SelectedRows = new List<DataGridViewRow>();
            foreach (DataGridViewRow dgvr in dataGridView1.SelectedRows)
            {
                SelectedRows.Add(dgvr);
            }
     
            SelectedRows.Sort(DataGridViewRowIndexCompare);
     
            for (int i = SelectedRows.Count - 1; i >= 0; i--)
            {
                int selRowIndex = SelectedRows[i].Index;
                if ((selRowIndex <= dataGridView1.Rows.Count - 1) && (!(selRowIndex == dataGridView1.Rows.Count - 1)))
                {
     
                    dataGridView1.Rows.Remove(SelectedRows[i]);
                    if ((selRowIndex + 1) == dataGridView1.Rows.Count)
                    {
                        dataGridView1.Rows.Add();
     
                    }
                    dataGridView1.Rows[selRowIndex].Selected = false;
                    dataGridView1.Rows.Insert(selRowIndex + 1, SelectedRows[i]);
                    dataGridView1.Rows[selRowIndex + 1].Selected = true;
     
                }
     
            }
     
        }
     
        private void buttonMultiRowUp_Click(object sender, EventArgs e)
        {
     
            if (dataGridView1.Rows.Count == 0) return;
     
            if (dataGridView1.SelectedRows.Count == 0)
            {
                if (dataGridView1.CurrentCell.RowIndex > -1)
                {
                    dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Selected = true;
                }
                else
                {
                    return;
                }
            }
     
            List<DataGridViewRow> SelectedRows = new List<DataGridViewRow>();
            foreach (DataGridViewRow dgvr in dataGridView1.SelectedRows)
            {
                SelectedRows.Add(dgvr);
            }
     
            SelectedRows.Sort(DataGridViewRowIndexCompare);
     
            for (int i = 0; i <= SelectedRows.Count - 1; i++)
            {
                int selRowIndex = SelectedRows[i].Index;
                if (selRowIndex > 0)
                {
                    //dataGridView1.Rows[selRowIndex].Selected = false;
                    
                    dataGridView1.Rows.Remove(SelectedRows[i]);
                    dataGridView1.Rows[selRowIndex].Selected = false;
                    //SelectedRows[i].Selected = false;
                    dataGridView1.Rows.Insert(selRowIndex - 1, SelectedRows[i]);
                    dataGridView1.CurrentCell.Selected = false;
                    dataGridView1.Rows[selRowIndex - 1].Selected = true;
     
                }
            }
     
            if (dataGridView1.SelectedRows.Count == 1)
            {
                dataGridView1.CurrentCell = dataGridView1.Rows[SelectedRows[0].Index].Cells[0];
            }
        }

    And this function for the sorting

     private static int DataGridViewRowIndexCompare(DataGridViewRow x, DataGridViewRow y)
        {
            if (x == null)
            {
                if (y == null)
                {
                    // If x is null and y is null, they're
                    // equal. 
                    return 0;
                }
                else
                {
                    // If x is null and y is not null, y
                    // is greater. 
                    return -1;
                }
            }
            else
            {
                // If x is not null...
                //
                if (y == null)
                // ...and y is null, x is greater.
                {
                    return 1;
                }
                else
                {
                    // ...and y is not null, compare the 
                    // lengths of the two strings.
                    //
                    int retval = x.Index.CompareTo(y.Index);
     
                    if (retval != 0)
                    {
                        // If the strings are not of equal length,
                        // the longer string is greater.
                        //
                        return retval;
                    }
                    else
                    {
                        // If the strings are of equal length,
                        // sort them with ordinary string comparison.
                        //
                        return x.Index.CompareTo(y.Index);
                    }
                }
            }
        }

    • Označen jako odpověď Mutekin 16. března 2012 6:14
    • Upravený Padma Lahore 16. března 2012 6:32
    •  
  • 16. března 2012 6:28
     
     

    Hi Mr Padma,

    It works like a charm :)

    Thanks again for your supports to that unique and very useful code. I am sure many will be use this sharing.

    See you later ...

    Mustafa

  • 16. března 2012 6:34
     
     

    You're welcome!

    I added the function for sorting to the post so that the code is complete and is easy to copy.

    Have a nice day, Mutekin.

  • 30. listopadu 2012 15:40
     
     

    Hi, I was looking for something like this as well albiet only really needed a single row move up and down function.  After adding your code it works for the MOST part for me however I have the following issue:

    When I select a single row and move it down to the bottom of the grid, just as I get near the bottom, it appends an empty row.  If I keep hitting my "Move Down" button it keeps adding empty rows.  I don't want that behavior.

    Also, If I select one of these empty rows and try to either move it up or down, it crashes the app.  Can you help?

    Thanks!


    • Upravený Marshall V 30. listopadu 2012 15:41
    •