如题。我写的代码是这样的:private void btnToUp_Click(object sender, EventArgs e)
        {
            DataGridViewRowCollection rows = this.gvBottom.Rows;
            DataGridViewSelectedRowCollection selectRows = this.gvBottom.SelectedRows;
            if (rows.Count > 0)
            {
                int rowIndex = selectRows[selectRows.Count - 1].Index;
                if (rowIndex == rows.Count)
                {
                    MessageBox.Show("当前已是第一行了!");
                    return;
                }
                int Rorder = int.Parse(selectRows[0].Cells["rorder"].Value.ToString());
                rows[rowIndex - 1].Cells["rorder"].Value = Rorder;
                for (int i = 0; i < selectRows.Count; i++)
                {
                    string num = (Rorder - (i + 1) * 10).ToString();
                    if (num.Length == 2)
                    {
                        num = "00" + num;
                    }
                    if (num.Length == 3)
                    {
                        num = "0" + num;
                    }
                    selectRows[i].Cells["rorder"].Value = num;                }
                this.gvBottom.Sort(this.gvBottom.Columns["rorder"], ListSortDirection.Ascending);
                isModify = 1;
            }
            else {
                MessageBox.Show("请选择您要调整的行!");
            }
        }
代码在运行过后,选择几行数据,第一次点击"向上调",调的是正确的,不关闭窗体,在重新选择几行数据,在点一次'向上调',调整过后就乱七八糟的了,比如说第一次,我选择序号为"50,60,70"的行,按向上调,这选择的3行序号就变成了40,50,60,而以前的40这个行序号就变成70,这就是我想要的结果,但是不关闭窗体,在重新选择几行数据,"60,70,80",点向上调,结果就乱七八糟的了,有时候还有重复的序号,这三行也没有调到50上面去,有时候可能是1行,或者2行跳的别的地方去了。有没有人能告诉我这是为什么,有什么办法解决。
还有,最号让光标也随着选择的行上调!谁能帮我解决了这个问题,80分全部给他。
灌水的不要来!

解决方案 »

  1.   


     private void Form3_Load(object sender, EventArgs e)
            {
                for (int i = 0; i < 10; i++)
                {
                    DataGridViewRow row = dataGridView1.Rows[dataGridView1.Rows.Add()];
                    row.Cells[0].Value = i;
                    row.Cells[1].Value = i + "B";
                    row.Cells[2].Value = i + "C";
                }        }        private void btnToUp_Click(object sender, EventArgs e)
            {
                DataGridViewSelectedRowCollection rows = dataGridView1.SelectedRows;
                if (rows.Count == 0)
                {
                    MessageBox.Show("未选择行!");
                    return;
                }            if (rows[0].Index == 0)
                {
                    MessageBox.Show("已是第一行!");
                    return;
                }            RowUp(rows, dataGridView1.Rows, "Column1");        }        private void RowUp(DataGridViewSelectedRowCollection selectRows2, DataGridViewRowCollection rows, string colName)
            {         
                DataGridViewRow[] selectRows = new DataGridViewRow[selectRows2.Count];            for (int i = 0; i < selectRows2.Count; i++)
                {
                    selectRows[i] = selectRows2[i];
                }            for (int i = 0; i < selectRows.Length; i++)
                {
                    for (int j = i + 1; j < selectRows.Length; j++)
                    {
                        if (selectRows[i].Index > selectRows[j].Index)
                        {
                            DataGridViewRow rwo = selectRows[i];
                            selectRows[i] = selectRows[j];
                            selectRows[j] = rwo;
                        }
                    }
                }
                int motionNumber = selectRows[0].Index - selectRows.Length < 0 ? 0 : selectRows[0].Index - selectRows.Length;
                if (motionNumber != 0)
                    motionNumber = selectRows[0].Index - motionNumber;
                else
                    motionNumber = selectRows[0].Index;            motionNumber = selectRows[0].Index - motionNumber;
                foreach (DataGridViewRow row in selectRows)
                {
                    for (int i = row.Index; i >= motionNumber; i--)
                    {                  
                        object value = row.Cells[colName].Value;
                        row.Cells[colName].Value = rows[i].Cells[colName].Value;
                        rows[i].Cells[colName].Value = value;
                    }
                    motionNumber++;                dataGridView1.Sort(Column1, ListSortDirection.Ascending);
                    Application.DoEvents();
                }        }
      

  2.   

    我新建个项目,不连数据库,datagridview值直接循环加进去,用了楼上的代码,完全可以达到我要的效果,
    但是不知道为什么,在我工作写的项目中,用楼上的代码就不行了,完全没效果,我用的是dataTable数据源的。
      

  3.   

    试了N久 老搞不定在表格绑定数据源之后
    使用
    dataGridView1.Sort(dataGridView1.Columns[colName], ListSortDirection.Ascending);
    选中行的选中状态变为-1;暂时没解决方法。但是你可以把绑定写成不绑定这样就能用上面的方法了。
      

  4.   

    我用你说的方法,循环数据源添加到datagridview中,不绑定数据,这样是可以的,但是又有个问题,我选择 5,6,7三行,点上调,这三行就跳到datagridview第一行后面,而不是原来的第3行后面,选择几行,就跳过几行直接到几行上面,我要的是选择的几行每次都跳过1行。
    你的代码有点复杂,我看的不是很明白,能不能给该下啊!
      

  5.   


     private void Form3_Load(object sender, EventArgs e)
            {
                for (int i = 0; i < 10; i++)
                {
                    DataGridViewRow row = dataGridView1.Rows[dataGridView1.Rows.Add()];
                    row.Cells[0].Value = i;
                    row.Cells[1].Value = i + "B";
                    row.Cells[2].Value = i + "C";
                }        }        private void btnToUp_Click(object sender, EventArgs e)
            {
                DataGridViewSelectedRowCollection rows = dataGridView1.SelectedRows;
                if (rows.Count == 0)
                {
                    MessageBox.Show("未选择行!");
                    return;
                }            RowUp(rows, dataGridView1.Rows, "Column1");        }        private void button2_Click(object sender, EventArgs e)
            {
                DataGridViewSelectedRowCollection rows = dataGridView1.SelectedRows;
                if (rows.Count == 0)
                {
                    MessageBox.Show("未选择行!");
                    return;
                }            RowNext(rows, dataGridView1.Rows, "Column1");
            }        private void RowUp(DataGridViewSelectedRowCollection selectRows2, DataGridViewRowCollection rows, string colName)
            {
                DataGridViewRow[] selectRows = new DataGridViewRow[selectRows2.Count];            for (int i = 0; i < selectRows2.Count; i++)
                {
                    selectRows[i] = selectRows2[i];
                }            for (int i = 0; i < selectRows.Length; i++)
                {
                    for (int j = i + 1; j < selectRows.Length; j++)
                    {
                        if (selectRows[i].Index > selectRows[j].Index)
                        {
                            DataGridViewRow rwo = selectRows[i];
                            selectRows[i] = selectRows[j];
                            selectRows[j] = rwo;
                        }
                    }
                }            if (selectRows[0].Index == 0)
                {
                    MessageBox.Show("已是第一行!");
                    return;
                }            int motionNumber = selectRows[0].Index - 1;
                foreach (DataGridViewRow row in selectRows)
                {
                    for (int i = row.Index - 1; i >= motionNumber; i--)
                    {
                        object value = row.Cells[colName].Value;
                        row.Cells[colName].Value = rows[i].Cells[colName].Value;
                        rows[i].Cells[colName].Value = value;
                    }
                    motionNumber++;                dataGridView1.Sort(dataGridView1.Columns[colName], ListSortDirection.Ascending);
                    Application.DoEvents();
                }
            }        private void RowNext(DataGridViewSelectedRowCollection selectRows2, DataGridViewRowCollection rows, string colName)
            {
                DataGridViewRow[] selectRows = new DataGridViewRow[selectRows2.Count];            for (int i = 0; i < selectRows2.Count; i++)
                {
                    selectRows[i] = selectRows2[i];
                }            for (int i = 0; i < selectRows.Length; i++)
                {
                    for (int j = i + 1; j < selectRows.Length; j++)
                    {
                        if (selectRows[i].Index < selectRows[j].Index)
                        {
                            DataGridViewRow rwo = selectRows[i];
                            selectRows[i] = selectRows[j];
                            selectRows[j] = rwo;
                        }
                    }
                }            if (selectRows[0].Index == rows.Count - 1)//这里的表格未启用添加,所以-1 启动的话得-2
                {
                    MessageBox.Show("已是最后一行!");
                    return;
                }            int motionNumber = selectRows[0].Index + 1;
                foreach (DataGridViewRow row in selectRows)
                {
                    for (int i = row.Index + 1; i <= motionNumber; i++)
                    {
                        object value = row.Cells[colName].Value;
                        row.Cells[colName].Value = rows[i].Cells[colName].Value;
                        rows[i].Cells[colName].Value = value;
                    }
                    motionNumber--;                dataGridView1.Sort(dataGridView1.Columns[colName], ListSortDirection.Ascending);
                    Application.DoEvents();
                }
            }    
      

  6.   

    http://topic.csdn.net/u/20090608/11/b985dae2-8a1e-48eb-945f-189b4a9e1f97.html