Hello Andy,
There is no easy way to do this, but this example will do everything you need:
using
System;
using
System.Collections.Generic;
using
System.Drawing;
using
System.Runtime.InteropServices;
using
System.Windows.Forms;
using
Telerik.WinControls.Data;
using
Telerik.WinControls.UI;
public
partial
class
Form1 : Form
{
private
RadGridView radGridView1;
private
List<Person> persons;
public
Form1()
{
InitializeComponent();
this
.Controls.Add(radGridView1 =
new
RadGridView());
radGridView1.Dock = DockStyle.Fill;
radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
radGridView1.GridBehavior =
new
CustomGridBehavior();
persons =
new
List<Person>();
for
(
int
i = 0; i < 20; i++)
{
if
(i % 4 == 0)
{
persons.Add(
new
Person
{
Id = i,
Name =
"L'Person "
+ i,
});
}
else
{
persons.Add(
new
Person
{
Id = i,
Name =
"Person "
+ i,
});
}
}
radGridView1.DataSource = persons;
}
private
class
CustomGridBehavior : BaseGridBehavior
{
public
override
bool
OnMouseDown(MouseEventArgs e)
{
var cell =
this
.GridControl.ElementTree.GetElementAtPoint(e.Location)
as
GridHeaderCellElement;
if
(cell !=
null
)
{
Keyboard.HoldShift();
}
return
base
.OnMouseDown(e);
}
public
override
bool
OnMouseUp(MouseEventArgs e)
{
if
(Keyboard.ShiftPressed)
{
Keyboard.ReleaseShift();
}
return
base
.OnMouseUp(e);
}
public
override
bool
ProcessKeyPress(KeyPressEventArgs keys)
{
if
(keys.KeyChar == (
char
)Keys.Space)
{
if
(
this
.GridControl.CurrentRow !=
null
&&
this
.GridControl.CurrentRow.DataBoundItem !=
null
)
{
var data =
this
.GridControl.CurrentRow.DataBoundItem
as
Person;
if
(data !=
null
)
{
data.Selected = !data.Selected;
this
.GridControl.CurrentRow.InvalidateRow();
}
}
}
return
base
.ProcessKeyPress(keys);
}
}
private
static
class
Keyboard
{
const
int
VK_SHIFT = 0x10;
const
uint
KEYEVENTF_KEYUP = 0x2;
static
bool
shiftPressed =
false
;
public
static
bool
ShiftPressed
{
get
{
return
Keyboard.shiftPressed;
}
}
[DllImport(
"user32.dll"
)]
public
static
extern
void
keybd_event(
byte
bVk,
byte
bScan,
uint
dwFlags,
uint
dwExtraInfo);
public
static
void
HoldShift()
{
if
(!shiftPressed)
{
keybd_event((
byte
)VK_SHIFT, 0, 0, 0);
shiftPressed =
true
;
}
}
public
static
void
ReleaseShift()
{
if
(shiftPressed)
{
keybd_event((
byte
)VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
shiftPressed =
false
;
}
}
}
}
public
class
Person
{
public
int
Id
{
get
;
set
;
}
public
string
Name
{
get
;
set
;
}
public
bool
Selected
{
get
;
set
;
}
}
Hope this helps, if you have any other questions or comments, please let me know,
Best Regards,
Emanuel Varga
Telerik WinForms MVP