本实例主要实现在同一 GridView 中实现多单元格的复制、黏贴和撤销
实现如下功能:
首先设置
gridView2.OptionsBehavior.Editable = false;
gridView2.OptionsSelection.MultiSelect = true;
gridView2.OptionsSelection.MultiSelect = true;
代码如下:
/// <summary>
/// 对用户的按键进行相关的操作。
/// </summary>
/// 对用户的按键进行相关的操作。
/// </summary>
private void gridControl2_KeyDown(object sender, KeyEventArgs e)
{
if ((e.Control == true) && e.KeyCode == Keys.C)
{
_cells = CopyDatas((sender as GridControl).FocusedView as GridView);
_isControlV = false;
}
else if ((e.Control == true) && e.KeyCode == Keys.V)
{
PasteDatas(_cells, sender as GridControl);
}
else if ((e.Control == true) && e.KeyCode == Keys.Z)
{
UndoDatas(sender as GridControl);
}
}
{
if ((e.Control == true) && e.KeyCode == Keys.C)
{
_cells = CopyDatas((sender as GridControl).FocusedView as GridView);
_isControlV = false;
}
else if ((e.Control == true) && e.KeyCode == Keys.V)
{
PasteDatas(_cells, sender as GridControl);
}
else if ((e.Control == true) && e.KeyCode == Keys.Z)
{
UndoDatas(sender as GridControl);
}
}
#region 私有方法
/// <summary>
/// 获取复制组合键所选的单元格。
/// </summary>
/// <param name="gridView">当前的 GridView。</param>
/// <returns>选中的单元格数组。</returns>
private GridCell[] CopyDatas(GridView gridView)
{
GridCell[] returnCells = gridView.GetSelectedCells();
return returnCells;
}
/// <summary>
/// 撤销操作。
/// </summary>
/// <param name="gridcontrol">当前的 GridControl。</param>
private void UndoDatas(GridControl gridcontrol)
{
DataTable dt = gridcontrol.DataSource as DataTable;
if (_isControlV == false || _undodata.Count <= 0)
return;
foreach (GridSelectData item in _undodata)
{
dt.Rows[item.RowIndex][item.ColumnName] = item.CellValue;
}
gridcontrol.RefreshDataSource();
_isControlV = false;
(gridcontrol.FocusedView as GridView).ClearSelection();
(gridcontrol.FocusedView as GridView).SelectCell(_pasteStartCell);
}
/// <summary>
/// 黏贴操作。
/// </summary>
/// <param name="sourceCells">复制操作时获得单元格集合。</param>
/// <param name="gridcontrol">当前的 GridControl。</param>
private void PasteDatas(GridCell[] sourceCells, GridControl gridcontrol)
{
_undodata.Clear();
if (sourceCells == null || sourceCells.Count() <= 0)
return;
#region 局部变量
//当前操作的GridView
GridView gridView = gridcontrol.FocusedView as GridView;
//黏贴位置的起始单元格
GridCell pasteStartCell = gridView.GetSelectedCells()[0];
_pasteStartCell = pasteStartCell;
//获取GridView 中显示的的列集合
GridColumnReadOnlyCollection visibleColumnsCollection = gridView.VisibleColumns;
//获取黏贴处的行索引
int pasteStartRowIndex = pasteStartCell.RowHandle;
//获取黏贴处的列索引
int startColumnIndex = pasteStartCell.Column.VisibleIndex;
//黏贴处的列的名称集合
List<string> columnsName = new List<string>();
//获取GridView 的数据源
DataTable dt = gridcontrol.DataSource as DataTable;
//复制的单元格的列数
int sourceColumnCount = 0;
//复制的单元格的行数
int sourceRowCount = 0;
#endregion
#region 获取复制数据中的行数和列数
foreach (GridCell item in sourceCells)
{
if (item.RowHandle == sourceCells[0].RowHandle)
sourceColumnCount++;
else
break;
}
sourceRowCount = sourceCells.Count() / sourceColumnCount;
#endregion
#region 界限判断
if (dt.Rows.Count < pasteStartRowIndex + sourceRowCount)//判断添加的行是否超出表的行数
throw new Exception("超出表的行数!");
if (pasteStartCell.Column.VisibleIndex + sourceColumnCount > visibleColumnsCollection.Count)//判断添加的累列是否超出视图中的列的行数
throw new Exception("超出表的列数!");
#endregion
for (int k = startColumnIndex; k < startColumnIndex + sourceColumnCount; k++)
columnsName.Add(visibleColumnsCollection[k].FieldName);
for (int j = 0; j < sourceRowCount; j++)
{
int temp = gridView.GetDataSourceRowIndex(pasteStartRowIndex + j);
for (int p = 0; p < columnsName.Count(); p++)
{
//保留删除的数据,以便撤销操作恢复数据
GridSelectData undodataTemp = new GridSelectData(temp, columnsName[p], dt.Rows[temp][columnsName[p]].ToString());
_undodata.Add(undodataTemp);
string tempstring = gridView.GetRowCellValue(_cells[j * sourceColumnCount + p].RowHandle, _cells[j * sourceColumnCount + p].Column).ToString();
dt.Rows[temp][columnsName[p]] = tempstring;
}
}
gridcontrol.RefreshDataSource();
_isControlV = true;
gridView.SelectCells(pasteStartCell.RowHandle, pasteStartCell.Column, pasteStartCell.RowHandle + sourceRowCount - 1, visibleColumnsCollection[pasteStartCell.Column.AbsoluteIndex + sourceColumnCount - 1]);
}
#endregion
创建存储撤销数据的类
/// <summary>
/// 存放指定单元格数据。
/// </summary>
class GridSelectData
{
#region 字段与属性
private int _rowIndex;
/// <summary>
/// 获取或设置行索引。
/// </summary>
public int RowIndex
{
get { return _rowIndex; }
set { _rowIndex = value; }
}
private string _columnName;
/// <summary>
/// 获取或设置列名称。
/// </summary>
public string ColumnName
{
get { return _columnName; }
set { _columnName = value; }
}
private string _cellValve;
/// <summary>
/// 获取或设置单元格数据。
/// </summary>
public string CellValue
{
get { return _cellValve; }
set { _cellValve = value; }
}
#endregion
public GridSelectData()
{
}
/// <summary>
/// 存放指定单元格数据。
/// </summary>
/// <param name="rowIndex">行索引。</param>
/// <param name="columnName">列名称。</param>
/// <param name="cellValue">单元格数据。</param>
public GridSelectData(int rowIndex, string columnName, string cellValue)
{
_rowIndex = rowIndex;
_columnName = columnName;
_cellValve = cellValue;
}
}
/// 存放指定单元格数据。
/// </summary>
class GridSelectData
{
#region 字段与属性
private int _rowIndex;
/// <summary>
/// 获取或设置行索引。
/// </summary>
public int RowIndex
{
get { return _rowIndex; }
set { _rowIndex = value; }
}
private string _columnName;
/// <summary>
/// 获取或设置列名称。
/// </summary>
public string ColumnName
{
get { return _columnName; }
set { _columnName = value; }
}
private string _cellValve;
/// <summary>
/// 获取或设置单元格数据。
/// </summary>
public string CellValue
{
get { return _cellValve; }
set { _cellValve = value; }
}
#endregion
public GridSelectData()
{
}
/// <summary>
/// 存放指定单元格数据。
/// </summary>
/// <param name="rowIndex">行索引。</param>
/// <param name="columnName">列名称。</param>
/// <param name="cellValue">单元格数据。</param>
public GridSelectData(int rowIndex, string columnName, string cellValue)
{
_rowIndex = rowIndex;
_columnName = columnName;
_cellValve = cellValue;
}
}
还有不足之处,请多多指教