如何能让datagridview中的列移动(左移或右移)

思路:

1.   先在当前列的左边或者右边插入一个空列
2.   按行将当前列(被移动列)的值复制到 新列
3.   移除当前列

 

代码如下:

        ///


        /// 获取当前列
        ///

        public DataGridViewColumn CurCol
        {
            get
            {
                if (neoDataGrid1.CurrentCell != null)
                {
                    int col_index = neoDataGrid1.CurrentCell.ColumnIndex;                   
                    return neoDataGrid1.Columns[col_index];
                }
                return null;
            }
         
        }
    

        ///


        /// 按行将数据复制到新网格列
        ///

        /// 源列
        /// 目地列
        public DataGridViewColumn CopyDataGridColumn(DataGridView dgList, DataGridViewColumn colFrom, DataGridViewColumn colTo)
        {
            //DataGridViewColumn colTo = null;
            foreach (DataGridViewRow dgRow in dgList.Rows)
            {
                foreach (DataGridViewCell dgCell in dgRow.Cells)
                {
                    if (dgCell.ColumnIndex == colFrom.Index)
                    {
                        //dgRow.Cells[colTo.Name].Value = dgCell.Value;
                        dgRow.Cells[colTo.Index].Value = dgCell.Value;
                        break;
                    }
                }
            }

            return null;
        }

        ///


        /// 移动列
        ///

        /// DataGridView 控件
        /// 当前列
        /// 移动方向
        /// 移动位置
        public void MoveDataGridColumn(DataGridView dgList, DataGridViewColumn curCol,
            NNERP.NEOCommon.EnumPublic.EnumSelectAction direction,int pos)
        {
            DataGridViewTextBoxColumn temp = new DataGridViewTextBoxColumn();
            temp.Name = "temp";
            temp.HeaderText = curCol.HeaderText;
            if (direction.ToString() == "MoveFirst" || direction.ToString() == "MovePrevious")
            {
                if (curCol.Index >= 1 && curCol.Index <= dgList.Columns.Count - 1)
                {
                    //插入一个新列
                    dgList.Columns.Insert(pos, temp);

                    //复制到新列
                    CopyDataGridColumn(dgList, curCol, temp);

                    //移除原来那一列
                    dgList.Columns.RemoveAt(curCol.Index);
                }
            }
            else if(direction.ToString() == "MoveNext" || direction.ToString() == "MoveLast")
            {
                if (curCol.Index >= 0 && curCol.Index <= dgList.Columns.Count - 1)
                {
                    //插入一个新列
                    dgList.Columns.Insert(pos, temp);

                    //复制到新列
                    CopyDataGridColumn(dgList, curCol, temp);

                    //移除原来那一列
                    dgList.Columns.RemoveAt(curCol.Index);

                }
            }
        }
              
        ///


        /// 移动列
        ///

        /// DataGridView 控件
        /// 当前列
        /// 移动方向
        public void MoveDataGridColumn(DataGridView dgList,DataGridViewColumn col,NNERP.NEOCommon.EnumPublic.EnumSelectAction direction)
        {

            DataGridViewTextBoxColumn temp = new DataGridViewTextBoxColumn();           
            temp.Name="temp";
            temp.HeaderText = col.HeaderText;
          
            switch (direction.ToString())
            {
                case "MoveFirst":
                    MoveDataGridColumn(dgList, col,direction, 0);
                    break;                
                 
                case "MoveLast":
                    MoveDataGridColumn(dgList, col, direction, dgList.Columns.Count - 1);                 
                    break;

                case "MoveNext":
                    MoveDataGridColumn(dgList, col, direction, col.Index + 2);                  
                    break;

                case "MovePrevious":
                    MoveDataGridColumn(dgList, col, direction, col.Index - 1);
                    break;

                default:
                    break;
            }//end swich

 


        }

 

调用:

        private void 左移ToolStripMenuItem_Click(object sender, EventArgs e)
        {           
            MoveDataGridColumn(neoDataGrid1, CurCol, NNERP.NEOCommon.EnumPublic.EnumSelectAction.MovePrevious);
        }

        private void 右移ToolStripMenuItem_Click(object sender, EventArgs e)
        {           
            MoveDataGridColumn(neoDataGrid1, CurCol, NNERP.NEOCommon.EnumPublic.EnumSelectAction.MoveNext);
        }

你可能感兴趣的:(Windows应用程序开发)