日期:2014-05-20  浏览次数:21176 次

求 DataGridview Row 资料任意上下移动对调
请问前辈们:我有一个 DataGridview 里面假设有 6笔资料 3攔

  A B C
1
2
3
4
5
6

我要如何做到用鼠标将这6笔资料任意上下移动对调呢?  
那里有范例可以看呢?感谢~

------解决方案--------------------
参考
------解决方案--------------------
C# code
   /// <summary>
        /// 上一一条数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_up_Click(object sender, EventArgs e)
        {
            if (dgvCaseStateManage.SelectedRows.Count == 0)
            {
                CommonApp.FormMessageBox.Show(CommonApp.LoadMode.Prompt, "请选择要移动的数据");
                this.Focus();
                return;
            }
            int rowindex = dgvCaseStateManage.SelectedRows[0].Index;
            if (rowindex > 0)
            {

                mo.Csm_ID = dgvCaseStateManage.SelectedRows[0].Cells[0].Value.ToString();
                mo.Csm_Oderid = int.Parse(dgvCaseStateManage.SelectedRows[0].Cells[2].Value.ToString());


                model.Csm_ID = this.dgvCaseStateManage.Rows[rowindex - 1].Cells[0].Value.ToString();
                model.Csm_Oderid = int.Parse(this.dgvCaseStateManage.Rows[rowindex - 1].Cells[2].Value.ToString());
                opCaseState.Move(mo, model);

                GetAllWorkInfo();
                foreach (DataGridViewRow dgvr in dgvCaseStateManage.SelectedRows)
                {
                    dgvr.Selected = false;
                }
                dgvCaseStateManage.Rows[rowindex - 1].Selected = true;
                dgvCaseStateManage.CurrentCell = dgvCaseStateManage.Rows[rowindex - 1].Cells[1];
            }

        }


        /// <summary>
        /// 下一一条数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_down_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)dgvCaseStateManage.DataSource;
            if (dgvCaseStateManage.SelectedRows.Count == 0)
            {
                CommonApp.FormMessageBox.Show(CommonApp.LoadMode.Prompt, "请选择要移动的数据");
                this.Focus();
                return;
            }
            int rowindex = dgvCaseStateManage.SelectedRows[0].Index;
            if (rowindex < dt.Rows.Count - 1)
            {

                mo.Csm_ID = dgvCaseStateManage.SelectedRows[0].Cells[0].Value.ToString();
                mo.Csm_Name = dgvCaseStateManage.SelectedRows[0].Cells[1].Value.ToString();
                mo.Csm_Oderid = int.Parse(dgvCaseStateManage.SelectedRows[0].Cells[2].Value.ToString());

                model.Csm_ID = this.dgvCaseStateManage.Rows[rowindex + 1].Cells[0].Value.ToString();
                model.Csm_Oderid = int.Parse(this.dgvCaseStateManage.Rows[rowindex + 1].Cells[2].Value.ToString());


                opCaseState.Move(mo, model);

                GetAllWorkInfo();
                foreach (DataGridViewRow dgvr in dgvCaseStateManage.SelectedRows)
                {
                    dgvr.Selected = false;
                }
                dgvCaseStateManage.Rows[rowindex + 1].Selected = true;
                dgvCaseStateManage.CurrentCell = dgvCaseStateManage.Rows[rowindex + 1].Cells[1];
            }

        }
"update CaseStateManage set Csm_Oderid='{0}' where  Csm_ID= '{1}'";