Duplicate Ranges

 Hi

I am using the grid control where the user is required to select a range of cells which the programe then performs an action based on the selected range or ranges.

 My problem is as follows:

 Say the user selects cell 4 & 5 using the mouse. The grid will then create a range with the left and right cells as 4 & 5.

 The user then decides that the range should, in fact, include cell 6 so they select cells 4 to 6 using the Ctrl button and the mouse.

 On the screen it looks to the user that they have selected a single range of cells 4 to 6. However, the grid has created a second range with the left & right values as 4 & 6 so I now have two ranges, one for cells 4 & 5 and one for cells 4 to 6.

 Is there any way I can overcome this so the grid recognizes consecutive cells as a single range or is there another property I can use to get the selected cell values?

 Any help would be much appreciated.

 Regards

Andy

(PS. As a matter of interest if the users then again selects cells 4 to 6 using the mouse and the Ctrl button a third range will be created with the left cell as 4 and the right cell as 6 so in this case there would be three ranges with two of them duplicated)


3 Replies

AR Arulpriya Ramalingam Syncfusion Team January 10, 2018 11:19 AM UTC

Hi Andy,   
   
Thanks for contacting Syncfusion support.   
   
By default, multiple selections will be added to the SelectedRanges collection when control key is pressed. So, when expanding the ranges by using the control key, the expanded range will be added as new range. In order to achieve your scenario, the CurrentCellKeyDown event can be used. In that event, the selections can be cleared and added to the collection based on conditions. Please refer to the below code and sample,    
   
Code example   
   
//Event triggering   
this.gridControl1.CurrentCellKeyDown += GridControl1_CurrentCellKeyDown;   
   
//Event customization   
private void GridControl1_CurrentCellKeyDown(object sender, KeyEventArgs e)   
{   
    if (e.KeyData == (Keys.ControlKey | Keys.Control) && this.gridControl1.Selections.Count == 2)   
    {   
        //To get the selected ranges when multiple ranges are selected by using the control key   
        GridRangeInfo firstRange = this.gridControl1.Selections.Ranges[0];   
        GridRangeInfo secondRange = this.gridControl1.Selections.Ranges[1];   
        //To clear and the selected range as a single range   
        this.gridControl1.Selections.Clear();   
        this.gridControl1.Selections.Add(GridRangeInfo.Cells(firstRange.Top, firstRange.Left, secondRange.Bottom, secondRange.Right));   
        GridRangeInfo modifiedRange = this.gridControl1.Selections.Ranges[0];   
        MessageBox.Show("R{"+modifiedRange.Top.ToString() +"C"+ modifiedRange.Left.ToString() +"} : R{"+ modifiedRange.Bottom + "C"+ modifiedRange.Right+"}");   
    }   
}   
   
   
Regards,   
Arulpriya   



AN Andy January 10, 2018 07:05 PM UTC

Thanks for the reply, however, your solution clears all selected cells even those not in the range as in the scenario.

Lets say the user first selects cells 10 & 11. They then select 4 & 5 and then again 4, 5 & 6 as described in my original question. This results in three ranges created.
Range(0) L = 10 R=11
Range(1) L = 4 R = 5
Range(2) L = 4 R = 6

However, the result I need is two ranges of
Range(0) L = 10 R=11
Range(1) L = 4 R = 6

Kind regards

Andy




AR Arulpriya Ramalingam Syncfusion Team January 11, 2018 05:12 PM UTC

Hi Andy, 
 
Thanks for your update. 
 
We have analyzed all the possibility to achieve your scenario unfortunately the grid does not have the direct support or extensibility to identify the adjacent selections. However, the selections can be combined by using UnioinRange() method of the ActiveRange and the adjacent range can be manually by checking the active range and previous range. In order to retrieve the get the current selected range, the ActiveRange property can be used and the previous ranges can be retrieved from the Ranges collection based on the index. Please refer to the below code, 
 
Code example 
 
if (this.gridControl1.Selections.Ranges.Count > 0) 
{ 
    //To get the active range 
    GridRangeInfo activeRange = this.gridControl1.Selections.Ranges.ActiveRange; 
    int currentRangeIndex = this.gridControl1.Selections.Ranges.IndexOf(activeRange); 
    //To get the previous range 
    GridRangeInfo previousRange = this.gridControl1.Selections.Ranges[currentRangeIndex - 1]; 
    //To check for the right and bottom adjacent selections 
    if ((activeRange.Left - previousRange.Right) == 1 || (activeRange.Top - previousRange.Bottom) == 1) 
    { 
        //To combine the ranges 
        GridRangeInfo range = this.gridControl1.Selections.Ranges.ActiveRange.UnionRange(previousRange); 
        //To remove the duplicate ranges 
        this.gridControl1.Selections.Ranges.Remove(activeRange); 
        this.gridControl1.Selections.Ranges.Remove(previousRange); 
        //To add the combined ranges to the selection 
        this.gridControl1.Selections.Ranges.Add(range); 
    } 
} 
 
Note 
As in the above code snippet, you can manually check for the Top and Left adjacent selections. 
 
Please let us know, if you have any other queries. 
 
Regards, 
Arulpriya 


Loader.
Up arrow icon