GetSelectedRanges during GridSelectionChangedEventHandler

Hi, During GridSelectionChangedEventHandler event, I tried to GetSelectedRanges but I keep getting the previous range. As the event is named "...Changed...", I would assume that it should be triggered only after the selection has changed. Please see attached.

GetSelectedRanges_During_SelectionChanged.zip

5 Replies

AD Administrator Syncfusion Team July 4, 2006 04:07 PM UTC

Hi Leow, The reason is that selection is not cleared when the user moves the currentcell out of a selected range. You need to set the ExcelLikeCurrentCell behavior to TRUE. Here is a code snippet. this.gridControl1.ExcelLikeCurrentCell = true; Try this code to find the newly selected range in a grid . Here is a code snippet. private void gridControl1_SelectionChanged(object sender, Syncfusion.Windows.Forms.Grid.GridSelectionChangedEventArgs e) { if (e.Reason != GridSelectionReason.MouseDown && e.Reason != GridSelectionReason.Clear) { GridRangeInfoList g; gridControl1.Selections.GetSelectedRanges(out g, true); Console.WriteLine(g.Info + ":::::" + e.Reason); } } Let me know if this helps. Best Regards, Haneef


LK Leow Kah Man July 5, 2006 01:55 AM UTC

1. Select a cell, 2. hold the control button, select another cell. Your code will prevent the 2nd one from firing, because on the 2nd time, the e.Reason is MouseDown. If I choose to use only e.Reason != Clear, then I will see that MouseDown gets fired twice on (1).


LK Leow Kah Man July 5, 2006 06:10 AM UTC

if (!e.Range.IsEmpty && ( e.Reason == GridSelectionReason.SetCurrentCell || e.Reason == GridSelectionReason.ArrowKey || e.Reason == GridSelectionReason.MouseDown ) ) Perhaps this solves my problem. What do you think?


AD Administrator Syncfusion Team July 5, 2006 04:08 PM UTC

Hi Leow, Thanks for your update. The reason is that when you click the specified cell, the grid clears the old selection with e.Reason = GridSelectionReason.MouseDown and also add the new selection with the same e.Reason = GridSelectionReason.MouseDown. To find the selection type( Clear/Add selection ) with e.Reason = GridSelectionReason.MouseDown, you can use below to achieve this issue. if (e.Reason != GridSelectionReason.MouseDown && e.Reason != GridSelectionReason.Clear) Or if (!e.Range.IsEmpty && ( e.Reason == GridSelectionReason.SetCurrentCell || e.Reason == GridSelectionReason.ArrowKey || e.Reason == GridSelectionReason.MouseDown ) ) Let me know if this helps. Best Regards, Haneef


LK Leow Kah Man July 7, 2006 05:14 AM UTC

Yes it does, thank you.

Loader.
Up arrow icon