请问datagridview中怎样将行上移或下移?
当选中一行点击"向上"按钮,如果不是第一行,则向上移动,同时,
当选中一行点击"向下"按钮,如果不是最后一行,则可向下移动。
先谢过了。 

解决方案 »

  1.   

    如果移动是变换选中项,直接模拟鼠标上下一动就可以了。
    如果是移动数据,看下边的代码,测试通过,public partial class Form1 : Form
        {
            List<UserInfo> dataList = null;        /// <summary>
            /// Initializes a new instance of the <see cref="Form1"/> class.
            /// </summary>
            public Form1()
            {
                InitializeComponent();
                InitData();
            }        private void InitData()
            {
                //初始化20条数据
                dataList = new List<UserInfo>();            for (int i = 1; i <= 20; i++)
                {
                    UserInfo objUserInfo = new UserInfo();
                    objUserInfo.UID = i;
                    objUserInfo.UName = "姓名" + i;
                    dataList.Add(objUserInfo);
                }            //绑定数据到DataGridView
                dataGridView1.DataSource = dataList;
            }        /// <summary>
            /// 点击向上按钮触发事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button1_Click(object sender, EventArgs e)
            {
                //获取当前行
                int currentRowIndex = dataGridView1.CurrentRow.Index;            //如果当前行不是第一行
                if (currentRowIndex > 0)
                {
                    //获取当前行的上一行的索引
                    int nextRowIndex = currentRowIndex - 1;                //当前行绑定的数据对象
                    UserInfo currentUserInfo = dataList[currentRowIndex];
                    //下一行绑定的数据对象
                    UserInfo nextUserInfo = dataList[nextRowIndex];                //将数据对象位置互换
                    dataList[currentRowIndex] = nextUserInfo;
                    dataList[nextRowIndex] = currentUserInfo;                //重新绑定数据
                    dataGridView1.DataSource = dataList;
                    //当前行不选中
                    dataGridView1.Rows[currentRowIndex].Selected = false;
                    //选中当前行的下一行
                    dataGridView1.Rows[nextRowIndex].Selected = true;
                    //当前行设置为当前行的下一行
                    dataGridView1.CurrentCell = dataGridView1.Rows[nextRowIndex].Cells[0];
                    //刷新界面
                    dataGridView1.Refresh();
                }
            }        private void button2_Click(object sender, EventArgs e)
            {
                //获取当前行
                int currentRowIndex = dataGridView1.CurrentRow.Index;            //如果当前行不是最后一行
                if (currentRowIndex < dataGridView1.RowCount - 1)
                {
                    //获取当前行的下一行的索引
                    int nextRowIndex = currentRowIndex + 1;                //当前行绑定的数据对象
                    UserInfo currentUserInfo = dataList[currentRowIndex];
                    //下一行绑定的数据对象
                    UserInfo nextUserInfo = dataList[nextRowIndex];                //将数据对象位置互换
                    dataList[currentRowIndex] = nextUserInfo;
                    dataList[nextRowIndex] = currentUserInfo;                //重新绑定数据
                    dataGridView1.DataSource = dataList;
                    //当前行不选中
                    dataGridView1.Rows[currentRowIndex].Selected = false;
                    //选中当前行的下一行
                    dataGridView1.Rows[nextRowIndex].Selected = true;
                    //当前行设置为当前行的下一行
                    dataGridView1.CurrentCell = dataGridView1.Rows[nextRowIndex].Cells[0];
                    //刷新界面
                    dataGridView1.Refresh();
                }
            }
        }    public class UserInfo
        {
            public int UID
            {
                get;
                set;
            }        public string UName
            {
                get;
                set;
            }
        }
      

  2.   

    this.dataGridView1.DataSource = dataSet;
    this.dataGridView1.DataMember = "test";
    CurrencyManager cm;
    cm = (this.BindingContext[dataSet, "test"] as CurrencyManager);
    private void button1_Click(object sender, EventArgs e)
          {
            if (cm.Position == (cm.Count -1))
              cm.Position = 0;
            else
              cm.Position++;
          }
    http://topic.csdn.net/t/20060915/13/5024414.html