繁体   English   中英

如何使用功能区控件剪切,复制和粘贴网格的内容?

[英]How can i Cut,Copy and Paste the content of the Grid by using Ribbon Control?

我想使用功能区控件剪切,复制和粘贴网格的内容。

<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方法,即可完成技巧。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM