隐藏行时DisplayIndex读取错误

时间:2018-04-26 20:09:32

标签: c# wpf

目标是从选定的行中复制选定的单元格数据。

我这样做是通过捕获数据网格中的CopyingRowClipBoardContent事件并将其重定向到此代码:

var currentCell = e.ClipboardRowContent[VwrGrid.CurrentCell.Column.DisplayIndex];
e.ClipboardRowContent.Clear();
e.ClipboardRowContent.Add(currentCell);

这完美无缺!唯一的问题是,如果某些列被隐藏,DisplayIndex会不正确地读取。

因此,如果我们有第1项,第2项和第3项。

如果全部显示并且我选择了item3并将其复制,我将获得第3项中的单元格值。

问题是,如果项目2折叠/未显示,则复制项目3将告诉您正在尝试复制越界。因为它从左边开始计算displayIndex,3,只显示了两个。所以它被移到了桌子之外

No such field as index

2 个答案:

答案 0 :(得分:1)

对于WPF Datagrid,试试这个:

// The clipboard row works only for visible cells
// To obtain the data column use the columnIndex and then map that to the Columns collection
int columnIndex = dataGrid.CurrentCell.Column.DisplayIndex;
var column = dataGrid.Columns[columnIndex];

// Now get the needed column 
var cellContent = e.ClipboardRowContent.Where(i => i.Column == column).First();
e.ClipboardRowContent.Clear();
e.ClipboardRowContent.Add(cellContent);

对于Winforms:

请改用.Index.DisplayIndex仅适用于可见列。

答案 1 :(得分:0)

因为这是WPF并且我不能简单地使用索引,所以我只是遍历列并计算其可见性设置为折叠到我们试图获取displayindex的列的列数。然后从displayIndex中减去该数字。

name