
Jorge Gonzalez
Top achievements
Rank 1
Jorge Gonzalez
asked on 21 Dec 2009, 09:50 PM
Hello,
I need to capture the ctrl and shift keys after selecting a row on the grid. I can code something using the SelectionChanged and keydown events but it will take a while to perfect. Has anyone already coded this logic?
Thanks
4 Answers, 1 is accepted
0
Hi,
Can you provide more info about your scenario and desired result? We'll gladly help you.
Kind regards,
Vlad
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Can you provide more info about your scenario and desired result? We'll gladly help you.
Kind regards,
Vlad
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0

Jorge Gonzalez
Top achievements
Rank 1
answered on 23 Dec 2009, 10:26 PM
Hello Vlad,
I need to implement enhanced multi-select. Currently if multi-select is turned on and several rows are selected and the user clicks on a row without first pressing CTRL or SHIFT keys all rows are de-selected. My user does not want all rows to be de-selected. Imagine they've selected 10 rows and accidentally clicked on some row. They would have to go back and select again. I've coded this logic in a window with the Janus grid on it but I wasn't to sure what events to use in the Telerik grid. I think I've figured it out.
It sure would be nice if some property existed on the grid that allowed me to do this by setting the property to true or false.
Thanks Anyway
0

Jorge Gonzalez
Top achievements
Rank 1
answered on 13 Jan 2010, 05:52 AM
Hello,
I've finally figured out how to implement the enhanced multiselect feature.The problem with multiselect is that if you select multiple rows and than accidentally click on a row you lose all the hi-lighting of the rows. My solution sets the background of the row to red. The three grid events I used were PreviewMouseLeftButtonUp, PreviewKeyDown, and PreviewKeyUp. I used these events because the MouseLeftButtonUp was not working nor was the MouseRightButtonUp or the other non-Preview mouse events. It's probably a newbie mistake.
To select one row press the ctrl key and click on the row you want to hi-light or remove the hi-light from.
To select multiple rows click on a row (an anchor row) and then press the shift key and at the same time select the ending row.
Note. radGridViewhorz is the name of my grid.
Note. LastRowSelected is used to keep track of the last row selected. It's used when multiple rows are selected at one time using the shift key.
Here's the code:
private bool shiftKeyPressed;
private bool ctrlKeyPressed;
private int lastRowSelected = -1;
public bool CtrlKeyPressed
{
get { return ctrlKeyPressed; }
set { ctrlKeyPressed = value; }
}
public bool ShiftKeyPressed
{
get { return shiftKeyPressed; }
set { shiftKeyPressed = value; }
}
public int LastRowSelected
{
get { return lastRowSelected; }
set { lastRowSelected = value; }
}
private void radGridViewhorz_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
int numColumns;
int numRows;
int currRow;
int startRow;
int endRow;
Brush currRowColor;
numColumns = radGridViewhorz.Columns.Count;
if (numColumns == 0)
return;
numRows = radGridViewhorz.Items.Count;
if (numRows == 0)
return;
if(radGridViewhorz.SelectedItems.Count == 0)
return;
currRow = radGridViewhorz.Items.IndexOf(radGridViewhorz.SelectedItems[0]);
if (currRow < 0)
return;
if (ShiftKeyPressed == true)
{
if (LastRowSelected < 0)
LastRowSelected = currRow;
if (currRow >= LastRowSelected)
{
startRow = LastRowSelected;
endRow = currRow;
}
else
{
startRow = currRow;
endRow = LastRowSelected;
}
startRow++;
endRow++;
for (int i = startRow; i <= endRow; i++)
radGridViewhorz.ChildrenOfType<GridViewRow>()[i].Background = Brushes.Red;
LastRowSelected = currRow;
}
else if (CtrlKeyPressed == true)
{
currRowColor = radGridViewhorz.ChildrenOfType<GridViewRow>()[(currRow + 1)].Background;
if (currRowColor == Brushes.Red)
radGridViewhorz.ChildrenOfType<GridViewRow>()[(currRow + 1)].Background = Brushes.White;
else
radGridViewhorz.ChildrenOfType<GridViewRow>()[(currRow + 1)].Background = Brushes.Red;
LastRowSelected = currRow;
}
else
LastRowSelected = currRow;
}
private void radGridViewhorz_PreviewKeyDown(object sender, KeyEventArgs e)
{
switch (e.Key)
{
case Key.LeftShift:
ShiftKeyPressed = true;
break;
case Key.RightShift:
ShiftKeyPressed = true;
break;
case Key.LeftCtrl:
CtrlKeyPressed = true;
break;
case Key.RightCtrl:
CtrlKeyPressed = true;
break;
default:
break;
}
}
private void radGridViewhorz_PreviewKeyUp(object sender, KeyEventArgs e)
{
switch (e.Key)
{
case Key.LeftShift:
ShiftKeyPressed = false;
break;
case Key.RightShift:
ShiftKeyPressed = false;
break;
case Key.LeftCtrl:
CtrlKeyPressed = false;
break;
case Key.RightCtrl:
CtrlKeyPressed = false;
break;
default:
break;
}
}
To select all the rows at once I have a checkbox on the window, which when checked executes the checked event which executes this:
private void selectallhorz_Checked(object sender, RoutedEventArgs e)
{
foreach (GridViewRow row in radGridViewhorz.ChildrenOfType<GridViewRow>() )
row.Background = Brushes.Red;
}
To remove the highlighting from all the rows the unchecked event event of the checkbox executes this:
private void selectallhorz_Unchecked(object sender, RoutedEventArgs e)
{
foreach (GridViewRow row in radGridViewhorz.ChildrenOfType<GridViewRow>())
row.Background = Brushes.White;
}
0

Charlie
Top achievements
Rank 2
answered on 13 Apr 2011, 06:32 PM
I found this approach to be buggy.
This type of logic worked better for me:
This type of logic worked better for me:
bool ctrlKeyDown = false;
if (((Keyboard.GetKeyStates(Key.RightCtrl) & KeyStates.Down) > 0) ||
((Keyboard.GetKeyStates(Key.LeftCtrl) & KeyStates.Down) > 0))
{
ctrlKeyDown = true;
}