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];//設定當前行

                }

            }

 

        }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM