DataGridView上下移动行及设置当前行

//方法 上移 下移 删除 dGVshowProcess是一个DataGridView

        private void upOrdownOrDelete(string type)

        {           

            if (this.dGVshowProcess.CurrentRow == null)

            {

                MessageBox.Show("请选择要需要操作的工序所在行");

            }

            else if(type=="del")//删

            {

                if (MessageBox.Show("确定要删除吗?", "警告", MessageBoxButtons.YesNo) == DialogResult.Yes)

                {

                    this.dGVshowProcess.Rows.Remove(this.dGVshowProcess.CurrentRow);

                }

            }

            else if(type=="up")//上

            {

                if (this.dGVshowProcess.CurrentRow.Index <= 0)

                {

                    MessageBox.Show("此工序已在顶端,不能再上移!");

                }

                else

                {                                 

                    int nowIndex = this.dGVshowProcess.CurrentRow.Index;

                    object[] _rowData = (this.dGVshowProcess.DataSource as DataTable).Rows[nowIndex].ItemArray;

                    (this.dGVshowProcess.DataSource as DataTable).Rows[nowIndex].ItemArray = (this.dGVshowProcess.DataSource as DataTable).Rows[nowIndex - 1].ItemArray;

                    (this.dGVshowProcess.DataSource as DataTable).Rows[nowIndex-1].ItemArray = _rowData;                  

                    this.dGVshowProcess.CurrentCell = this.dGVshowProcess.Rows[nowIndex - 1].Cells[0];//设定当前行

                }

            }

            else if (type == "down")//下

            {

                if (this.dGVshowProcess.CurrentRow.Index >= this.dGVshowProcess.Rows.Count-1)

                {

                    MessageBox.Show("此工序已在底端,不能再下移!");

                }

                else

                {                  

                    int nowIndex = this.dGVshowProcess.CurrentRow.Index;

                    object[] _rowData = (this.dGVshowProcess.DataSource as DataTable).Rows[nowIndex].ItemArray;

                    (this.dGVshowProcess.DataSource as DataTable).Rows[nowIndex].ItemArray = (this.dGVshowProcess.DataSource as DataTable).Rows[nowIndex + 1].ItemArray;

                    (this.dGVshowProcess.DataSource as DataTable).Rows[nowIndex+1].ItemArray = _rowData;

                    this.dGVshowProcess.CurrentCell = this.dGVshowProcess.Rows[nowIndex + 1].Cells[0];//设定当前行

                }

            }

 

        }

你可能感兴趣的:(datagridview)