5

我正在使用 WPF Datagrid,并且正在尝试增强/更改复制和粘贴机制。

当用户选择一些单元格然后按 CTRL + C 时,底层控件能够捕获CopyingRowClipboardContent事件。

 this.mainDataGrid.CopyingRowClipboardContent 
              += this.DatagridOnCopyingRowClipboardContent;

在这种方法中,一些单元格被添加到标题和行中,因此导致“更宽”的网格。

    private void DatagridOnCopyingRowClipboardContent(
        object sender, 
        DataGridRowClipboardEventArgs dataGridRowClipboardEventArgs)
    {
        // this is fired every time a row is copied
        var allContent = dataGridRowClipboardEventArgs.ClipboardRowContent;

        allContent.Insert(0, new DataGridClipboardCellContent(
                                            null, 
                                            this.mainDataGrid.Columns[0], 
                                            "new cell"));
    }

此时我被卡住了,因为我试图在标题之前添加额外的一行,在最后一行之后添加两行(见下图)。

有任何想法吗?建议?

请注意,我对这里的 MVVM 方式不感兴趣。

在此处输入图像描述

4

3 回答 3

4

这是一个可能对您有所帮助的代码片段。此代码段主要用于检索您选择的所有数据,包括标题(我删除了该RowHeaders部分,因为您显然不需要它)。如果您有任何问题,请告诉我。我留下了一些用大写字母写的注释:这是您应该添加自己的数据的地方 这种方法的好处是它直接与您DataGrid的 'sItemsSource而不是DataGridCell. 主要原因是:DataGridCell例如,如果您使用格式化的数字,您将不会获得实际值,而只会获得格式化的值(假设您的源是 14.49,而您的源StringFormat是 N0,如果您使用 "常规”方式)

   /// <summary>
    /// Handles DataGrid copying with headers
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void OnCopyingDataGrid(object sender, ExecutedRoutedEventArgs e)
    {
        // First step: getting the coordinates list of all cells selected
        IList<Tuple<int, int>> cellsCoordinatesList = new List<Tuple<int, int>>();
        HashSet<int> rowList = new HashSet<int>();
        HashSet<int> columnList = new HashSet<int>();
        foreach (System.Windows.Controls.DataGridCellInfo cell in this.SelectedCells)
        {
            int column = cell.Column.DisplayIndex;
            int row = this.Items.IndexOf(cell.Item);
            cellsCoordinatesList.Add(new Tuple<int, int>(row, column));
            if (!rowList.Contains(row))
            {
                rowList.Add(row);
            }
            if (!columnList.Contains(column))
            {
                columnList.Add(column);
            }
        }

        // Second step: Create the table to copy/paste
        object[,] arrayToBeCopied = new object[rowList.Count, columnList.Count + 1];
        IList<string> colHead = this.ColumnHeaders.Cast<object>().Select(h => h.ToString()).ToList();
        for (int row = 0; row < arrayToBeCopied.GetLength(0); row++)
        {
            for (int column = 0; column < arrayToBeCopied.GetLength(1); column++)
            {
                if (row == 0)
                {
                    arrayToBeCopied[row, column] = colHead[columnList.ElementAt(column - 1)];
                }
                else
                {
                    arrayToBeCopied[row, column] = // WHATEVER YOU WANT TO PUT IN THE CLIPBOARD SHOULD BE HERE. THIS SHOULD GET SOME PROPERTY IN YOUR ITEMSSOURCE

                }
            }
        }

        // Third step: Converting it into a string
        StringBuilder sb = new StringBuilder();

        // HERE, ADD YOUR FIRST ROW BEFORE STARTING TO PARSE THE COPIED DATA

        for (int row = 0; row < arrayToBeCopied.GetLength(0); row++)
        {
            for (int column = 0; column < arrayToBeCopied.GetLength(1); column++)
            {
                sb.Append(arrayToBeCopied[row, column]);
                if (column < arrayToBeCopied.GetLength(1) - 1)
                {
                    sb.Append("\t");
                }
            }
            sb.Append("\r\n");
        }

        // AND HERE, ADD YOUR LAST ROWS BEFORE SETTING THE DATA TO CLIPBOARD

        DataObject data = new DataObject();
        data.SetData(DataFormats.Text, sb.ToString());

        Clipboard.SetDataObject(data);
    }
于 2012-11-01T14:23:52.297 回答
0

您是否要稍后将内容复制到 Excel 中?如果是这样,这就是我所做的:

/// <summary>
/// Copy the data from the data grid to the clipboard
/// </summary>
private void copyDataOfMyDataGridToClipboard(object sender, EventArgs e)
{
    // Save selection
    int selectedRow = this.myDataGrid.SelectedRows[0].Index;

    // Select data which you would like to copy
    this.myDataGrid.MultiSelect = true;
    this.myDataGrid.SelectAll();

    // Prepare data to be copied (that's the interesting part!)
    DataObject myGridDataObject = this.myDataGrid.GetClipboardContent();
    string firstRow = "FirstRowCommentCell1\t"+ this.someDataInCell2 +"..\r\n";
    string lastTwoRows = "\r\nBottomLine1\t" + yourvariables + "\r\nBottomLine2";
    string concatenatedData = firstRow + myGridDataObject.GetText() + lastTwoRows;

    // Copy into clipboard
    Clipboard.SetDataObject(concatenatedData);

    // Restore settings
    this.myDataGrid.ClearSelection();
    this.myDataGrid.MultiSelect = false;

    // Restore selection
    this.myDataGrid.Rows[selectedRow].Selected = true;
}

就我而言,我有一些静态标题,可以很容易地与一些变量连接。重要的是\t要声明另一个单元格,\r\n声明下一行

于 2014-02-07T10:48:46.790 回答
0

我意识到这是一篇较旧的帖子,但为了完整起见,我发布了这个解决方案。我找不到关于将数据网格行复制到剪贴板的更新的问题。使用 Clipboard.SetData 掩盖了 ClipboardRowContent 的意图。

为了我的需要,我将我想要的行重新粘贴回 e.ClipboardRowContent 中。cell.Item 是我需要的每个选定行的所有信息。

提示:我没有做 e.ClipboardRowContent.Clear(); 使用 e.ClipboardRowContent 后。我之前正在清除并使用 DataGrid.SelectedItems 来构建行。

private void yourDataGrid_CopyingRowClipboardContent(object sender,    DataGridRowClipboardEventArgs e)
{
var dataGridClipboardCellContent = new List<DataGridClipboardCellContent>(); 

string prevCell = "";
string curCell = ""; 

foreach (DataGridClipboardCellContent cell in e.ClipboardRowContent)
{
    //Gives you access item.Item or item.Content here
    //if you are using your struct (data type) you can recast it here curItem = (yourdatatype)item.Item;        
    curItem = cell.Item.ToString(); 

    if (curCell != prevCell)
        dataGridClipboardCellContent.Add(new DataGridClipboardCellContent(item, item.Column, curCell)); 

    prevCell = curCell;  

}
e.ClipboardRowContent.Clear();

//Re-paste back into e.ClipboardRowContent, additionally if you have modified/formatted rows to your liking
e.ClipboardRowContent.AddRange(dataGridClipboardCellContent);

}

于 2017-02-14T15:49:36.863 回答