我DevExpress GridControl
有一个默认GridView
来显示表单中的记录。我已将值绑定到SQL数据库表中的GridControl
。在这里,我想使用鼠标单击任意组合键选择单列的值作为数组或类似的东西。我使用Google搜索并使用CheckBox
列来solution选择多行。
但是,我的情况有所不同。我想选择单列的值作为数组。
修改 我也在寻找一个答案,当点击列标题中的任意组合键时,突出显示所有单元格值(类似于我们在Microsoft Excel中有一个选项)。
提前致谢。
答案 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
事件; CalcHitInfo
检测列标题上的点击(请查看此问题:“Detect the column header region in the mouse down event of a grid)”以及此知识库文章:“Hit Information”; 然后使用GridView.SelectCell
更新选择
private void SelectCells(GridColumn column) {
for (int i = 0; i < column.View.RowCount; i++)
column.View.SelectCell(i, column);
}
如果您的网格视图可编辑,则单元格编辑器可能会窃取MouseDown
事件。在这种情况下,您需要按照本讨论中的描述重新配置网格:GridView - How to catch a cell click?