无法在WinForms DevExpress GridControl中选择单列的值

时间:2017-09-04 06:24:03

标签: c# winforms devexpress xtragrid devexpress-windows-ui

DevExpress GridControl有一个默认GridView来显示表单中的记录。我已将值绑定到SQL数据库表中的GridControl。在这里,我想使用鼠标单击任意组合键选择单列的值作为数组或类似的东西。我使用Google搜索并使用CheckBox列来solution选择多行。

但是,我的情况有所不同。我想选择单列的值作为数组。

修改 我也在寻找一个答案,当点击列标题中的任意组合键时,突出显示所有单元格值(类似于我们在Microsoft Excel中有一个选项)。

提前致谢。

1 个答案:

答案 0 :(得分:2)

我认为有一种内置的方法可以做到这一点。您可以遍历行并使用ColumnView.GetRowCellValue访问特定列中的单元格。

这样的事情应该有效:

//get the number of rows in a target view (gridView for example)
int rowsCount = gridView.DataRowCount;
//allocate an array for column values
var columnValues = new object[rowsCount];
//get column by field name
var column = gridView.Columns["ColumnName"];
//loop through rows and populate column values
for (int rowIndex = 0; rowIndex < rowsCount; rowIndex++)
{
    columnValues[rowIndex] = gridView.GetRowCellValue(rowIndex, column);
}

更新:您可以参考DevExpress知识库中的官方示例:How to select cells under by clicking the GridBand or the column header

您需要做什么:

如果您的网格视图可编辑,则单元格编辑器可能会窃取MouseDown事件。在这种情况下,您需要按照本讨论中的描述重新配置网格:GridView - How to catch a cell click?