Hi,
I have a grid and I want to select a block of contiguous cells programmatically. I tried something like this:
for (int i = startIndex; i <= endIndex; i++)
{
Cell cell = myGrid.Rows[i].Cells[0] as Cell;
cell.IsSelected = true;
}
But I end up with only a single cell selected – the last one in the loop. Setting cell.IsSelected to true fires the SelectedCellsCollectionChanged event and deselects the previously selected cell. It appears that I cannot apply “Selected” to a collection of cells.
Is there a way to select a collection of cells programmatically?
Thank you.
Thanks ! You made my weekend. It works like a charm!
Nicolas
Hi NikolasBe,
It is possible to try that:
CellBase c = this.grvLiveData.Rows[0].Cells[0];
CellBase c2 = this.grvLiveData.Rows[0].Cells[1];
this.grvLiveData.SelectionSettings.SelectedCells.Add((Cell)c);
this.grvLiveData.SelectionSettings.SelectedCells.Add((Cell)c2);
It is possible way to select multiple cells programmatically.
I hope that can help :-)
Regards!
Mihail
Hello,
could it be that I'm missing something else ?
If I use
foreach
(Cell C in
_selectedPastedCellsCollection)
//this.grvLiveData.SelectionSettings.SelectedCells.Add(C);
this.grvLiveData.Rows[C.Row.Index].Cells[C.Column].IsSelected = true; }
Then (as described in earlier posts), the last cell in the collection is selected.
However, If I use:
.grvLiveData.SelectionSettings.SelectedCells.Add(C);
//this.grvLiveData.Rows[C.Row.Index].Cells[C.Column].IsSelected = true;
then nothing is selected. I can see in debug mode that the SelectionSettings.SelectedCells contain items, but nothing is selected in the grid. Any help ?
Thanks,
Yes, there is a SelectedCellsCollection, which can be used for just that situation.
this.grid1.SelectionSettings.SelectedCells
So instead of using cell.IsSelected, you can use this.grid1.SelectionSettings.SelectedCells.Add(cell)
-SteveZ