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