简体   繁体   中英

Selective selecting cells in Datagrid WPF

I have datagrid wpf with options:

SelectionUnit="Cell"
SelectionMode="Extended"

Datagrid has 10 columns. What I need is to select only eg 1-4 and 8-10 columns - skip 5-7 columns when I draggind mouse pointer over all datagrid cells. Is it possible to do this? I tried raise SelectedCellsChanged event and remove items from DataGrid.SelectedCells but then I got exception:

This collection does not support changing values with specific indexes.

Further information: I have table with 10 columns xn rows. All columns are with text values. Rows represents employees. Columns represents days - some of them are Saturdays/Sundays which are not working days. Cells values can be the same in rows and columns. I would like to have possibility to select all cells by dragging mouse pointer over datagrid, but skip select of these saturdays/sundays which can be in the middle of columns.

You can select all cells for a specific column by using the index of the cells and using System.Collections.Generic.IList and System.Collections.Generic.KeyValuePair

for more info:

You can see here

In you DataGrid XAML, set the event handler for SelectedCellsChanged :

SelectedCellsChanged="CustomGrid_SelectedCellsChanged"

Now, in your code-behind, add the code for event handler as follow :

private void CustomGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
{
    foreach(DataGridCellInfo cellInfo in minorGrid.SelectedCells)  // For each selected cell
    {
        DataGridCell cell = GetDataGridCell(cellInfo); // get the cell
        string cellText = ((TextBlock)cell.Content).Text; // get the text of the cell
        if (cellText.ToLower().Contains("off"))  // If meets the conditino
            cell.IsSelected = false;  // unselect the cell           
    }
}

private DataGridCell GetCell(DataGridCellInfo cellInfo)
{
    var cellContent = cellInfo.Column.GetCellContent(cellInfo.Item);
    if (cellContent != null)
        return (DataGridCell)cellContent.Parent;

    return null;
}

When a cell is selected, then we don't get the DataGridCell object in the SelectedCells list, but we get DataGridCellInfo objects. To get the DataGridCell objects, I have provided a method. Once we get the cell object, we can perform logic on the cell. This will unselect all the cells from your selection which have the text "off", which might be the case on holidays :).

Similarly, though, you can also unselect whole column if the column header contains "saturday" or "sunday", based on how you named the columns. Just change the following lines

string cellText = ((TextBlock)cell.Content).Text; // get the text of the cell
if (cellText.ToLower().Contains("off"))  // If meets the conditino
    cell.IsSelected = false;  // unselect the cell 

to

string headerText = cell.Column.Header.ToString();
if (headerText.ToLower().Contains("saturday") || headerText.ToLower().Contains("sunday"))  // If meets the conditino
    cell.IsSelected = false;  // unselect the cell  

Hope it helps :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM