如何使用功能区控件剪切、复制和粘贴网格的内容
本文关键字:网格 复制 功能区 何使用 控件 | 更新日期: 2023-09-27 18:29:54
我想使用Ribbon控件剪切、复制和粘贴网格的内容。
<Ribbon>
<RibbonGroup>
<RibbonButton Command="local:ContextMenuCommands.Cut" Label="Cut"/>
<RibbonButton Command="local:ContextMenuCommands.Copy" Label="Copy"/>
<RibbonButton Command="local:ContextMenuCommands.Paste" Label="Paste"/>
</RibbonGroup>
</Ribbon>
这就是将内容从DataGridView复制到剪贴板的方法:
private void CopyDataGridViewToClipboard(ref DataGridView dgv)
{
string s = "";
DataGridViewColumn oCurrentCol = default(DataGridViewColumn);
//Get header
oCurrentCol = dgv.Columns.GetFirstColumn(DataGridViewElementStates.Visible);
do {
s += oCurrentCol.HeaderText + Strings.Chr(Keys.Tab);
oCurrentCol = dgv.Columns.GetNextColumn(oCurrentCol, DataGridViewElementStates.Visible, DataGridViewElementStates.None);
} while (!(oCurrentCol == null));
s = s.Substring(0, s.Length - 1);
s += Environment.NewLine;
//Get rows
foreach (DataGridViewRow row in dgv.Rows) {
oCurrentCol = dgv.Columns.GetFirstColumn(DataGridViewElementStates.Visible);
do {
if (row.Cells(oCurrentCol.Index).Value != null) {
s += row.Cells(oCurrentCol.Index).Value.ToString;
}
s += Strings.Chr(Keys.Tab);
oCurrentCol = dgv.Columns.GetNextColumn(oCurrentCol, DataGridViewElementStates.Visible, DataGridViewElementStates.None);
} while (!(oCurrentCol == null));
s = s.Substring(0, s.Length - 1);
s += Environment.NewLine;
}
//Put to clipboard
DataObject o = new DataObject();
o.SetText(s);
Clipboard.SetDataObject(o, true);
}
以下是如何从剪贴板粘贴到DataGrid视图的选定单元格
private void PasteClipboardValue()
{
//Show Error if no cell is selected
if (dataGridView1.SelectedCells.Count == 0)
{
MessageBox.Show("Please select a cell", "Paste",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
//Get the starting Cell
DataGridViewCell startCell = GetStartCell(dataGridView1);
//Get the clipboard value in a dictionary
Dictionary<int, Dictionary<int, string>> cbValue =
ClipBoardValues(Clipboard.GetText());
int iRowIndex = startCell.RowIndex;
foreach (int rowKey in cbValue.Keys)
{
int iColIndex = startCell.ColumnIndex;
foreach (int cellKey in cbValue[rowKey].Keys)
{
//Check if the index is within the limit
if (iColIndex <= dataGridView1.Columns.Count - 1
&& iRowIndex <= dataGridView1.Rows.Count - 1)
{
DataGridViewCell cell = dataGridView1[iColIndex, iRowIndex];
//Copy to selected cells if 'chkPasteToSelectedCells' is checked
if ((chkPasteToSelectedCells.Checked && cell.Selected) ||
(!chkPasteToSelectedCells.Checked))
cell.Value = cbValue[rowKey][cellKey];
}
iColIndex++;
}
iRowIndex++;
}
}
private DataGridViewCell GetStartCell(DataGridView dgView)
{
//get the smallest row,column index
if (dgView.SelectedCells.Count == 0)
return null;
int rowIndex = dgView.Rows.Count - 1;
int colIndex = dgView.Columns.Count - 1;
foreach (DataGridViewCell dgvCell in dgView.SelectedCells)
{
if (dgvCell.RowIndex < rowIndex)
rowIndex = dgvCell.RowIndex;
if (dgvCell.ColumnIndex < colIndex)
colIndex = dgvCell.ColumnIndex;
}
return dgView[colIndex, rowIndex];
}
private Dictionary<int, Dictionary<int, string>> ClipBoardValues(string clipboardValue)
{
Dictionary<int, Dictionary<int, string>>
copyValues = new Dictionary<int, Dictionary<int, string>>();
String[] lines = clipboardValue.Split(''n');
for (int i = 0; i <= lines.Length - 1; i++)
{
copyValues[i] = new Dictionary<int, string>();
String[] lineContent = lines[i].Split(''t');
//if an empty cell value copied, then set the dictionary with an empty string
//else Set value to dictionary
if (lineContent.Length == 0)
copyValues[i][0] = string.Empty;
else
{
for (int j = 0; j <= lineContent.Length - 1; j++)
copyValues[i][j] = lineContent[j];
}
}
return copyValues;
}
对于Cut命令,只需添加DataGrid的Clear方法,技巧就完成了。