Search code examples
c#winformsdevexpressxtragriddevexpress-windows-ui

Unable to select single column's values in WinForms DevExpress GridControl


I have DevExpress GridControl with one default GridView to show the records in my form. I have bound the values to GridControl from SQL Database table. Here, I want to select the single column's values as array or something similar using mouse click with any key combination. I have googled and get the solution to select multiple rows using CheckBox column.

But, my scenario is different. I would like to select single column's values as array.

EDIT: I am also looking for an answer which needs to be highlight all cell values when clicked with any key combination in column header (similar like we have an option in Microsoft Excel).

Thanks in advance.


Solution

  • I don' think that there is a built-in method to do that. You can loop through rows and use ColumnView.GetRowCellValue to access the cells from a particular column.

    Something like this should work:

    //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);
    }
    

    UPDATE: You can refer to the official example in DevExpress knowledge base: How to select cells under by clicking the GridBand or the column header.

    What you'll need to do:

    If your grid view is editable, cell editors might steal the MouseDown event. In this case, you'll need to reconfigure the grid as described in this discussion: GridView - How to catch a cell click?