如题。我写的代码是这样的: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.   

    C#的我没有,vb.net的有,你要不要参考一下。
      

  2.   

    datagridview绑定的数据源是从数据库中读的DataTable,上下调的时候,只是在datagridview中操作,还没有跟新数据库!
      

  3.   

    楼主应该用CurrentRow就来操作行,,他和SelectedRows看起来相似,,但是有区别的,,
      

  4.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsApplication40
    {
        public partial class Form1 : Form
        {
            DataGridView gvBottom = new DataGridView();
            public Form1()
            {
                InitializeComponent();            gvBottom.Columns.Add("rorder", "rorder");
                gvBottom.Columns.Add("c2", "c2");
                gvBottom.Rows.Add(new object[] { 1, 11 });
                gvBottom.Rows.Add(new object[] { 2, 22 });
                gvBottom.Rows.Add(new object[] { 3, 33 });
                gvBottom.Rows.Add(new object[] { 4, 44 });
                gvBottom.Rows.Add(new object[] { 5, 55 });
                gvBottom.Parent = this;
                gvBottom.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                Button btnToUp = new Button();
                btnToUp.Parent = this;
                btnToUp.Text = "up";
                btnToUp.Click += new EventHandler(btnToUp_Click);
                btnToUp.BringToFront();
            }        void btnToUp_Click(object sender, EventArgs e)
            {
                for (int i = gvBottom.SelectedRows.Count - 1; i >= 0; i--)
                {
                    DataGridViewRow Row = gvBottom.SelectedRows[i];
                    int NewRowIndex = Row.Index - 1;
                    if (NewRowIndex >= 0)
                    {
                        gvBottom.Rows.Remove(Row);
                        gvBottom.Rows.Insert(NewRowIndex, Row);
                        for (int j = Row.Index; j < gvBottom.Rows.Count; j++)
                            gvBottom.Rows[j].Cells["rorder"].Value = j + 1;
                    }
                    else
                    {
                        MessageBox.Show("所选的首行已经是第一行");
                        return;
                    }
                }
            }
        }
    }
      

  5.   

    DataRow[] rows = dataTable1.Select("", "ord asc");DataTable t = DataTable1.Clone();t.Clear();foreach (DataRow row in rows)    t.ImportRow(row);DataTable1 = t;