Using doubleclick events in the Devexpress Gridview Control is a simple task.
There are 2 different ways of doing this. The variant you choose, depends on the Gridview’s gridView.OptionsBehavior.Editable property. If this property is set to false you have to use the DoubleClick event. If this property is set to true you have to use the ShownEditor and HiddenEditor event. I will also give an example on how to combine both methods, if you are using both (true and false) on the gridView.OptionsBehavior.Editable.

Samples

OptionsBehavior.Editable set to False

C# Sample

private void gridView1_DoubleClick(object sender, EventArgs e)
{
	GridView view = (GridView)sender;
	Point pt = view.GridControl.PointToClient(MousePosition);
	GridHitInfo info = view.CalcHitInfo(pt);
	if ((info.InRow || info.InRowCell) && info.Column != null)
	{
		Object cellValue = view.GetRowCellValue(info.RowHandle, info.Column);
		MessageBox.Show(string.Format("Made a doubleclick at rowhandle: {0}, columnhandle: {1}, value: {2}.", info.RowHandle, info.Column.ColumnHandle, cellValue));
	}
}

VB.NET Sample

Private Sub GridView1_DoubleClick( sender As Object,  e As EventArgs) Handles GridView1.DoubleClick
        Dim view As GridView = DirectCast(sender, GridView)
        Dim pt As Point = view.GridControl.PointToClient(MousePosition)
        Dim info As GridHitInfo = view.CalcHitInfo(pt)
        If (info.InRow OrElse info.InRowCell) AndAlso info.Column IsNot Nothing Then
	        Dim cellValue As [Object] = view.GetRowCellValue(info.RowHandle, info.Column)
	        MessageBox.Show(String.Format("Made a doubleclick at rowhandle: {0}, columnhandle: {1}, value: {2}.", info.RowHandle, info.Column.ColumnHandle, cellValue))
        End If
End Sub

OptionsBehavior.Editable set to True

When inplace editing is enabled, a click on a grid cell activates the inplace editor (rowedit).
All mouse events will be sent to the editor and cannot be handled by the GridView directly. The DoubleClick event handler is no longer being executed.

C# Sample

private void gridView1_ShownEditor(object sender, EventArgs e)
{
	BaseEdit inplaceEditor = ((GridView)sender).ActiveEditor;
	inplaceEditor.DoubleClick += inplaceEditor_DoubleClick;
}

void inplaceEditor_DoubleClick(object sender, EventArgs e)
{
	BaseEdit editor = (BaseEdit)sender;
	GridControl grid = (GridControl)editor.Parent;
	Point pt = grid.PointToClient(Control.MousePosition);
	GridView view = (GridView)grid.FocusedView;
	GridHitInfo info = view.CalcHitInfo(pt);
	if ((info.InRow || info.InRowCell) && info.Column != null)
	{
		Object cellValue = view.GetRowCellValue(info.RowHandle, info.Column);
		MessageBox.Show(string.Format("Made a doubleclick at rowhandle: {0}, columnhandle: {1}, value: {2}.", info.RowHandle, info.Column.ColumnHandle, cellValue));
	}
}

private void gridView1_HiddenEditor(object sender, EventArgs e)
{
	if (sender.GetType() == typeof (BaseEdit))
	{
		BaseEdit editor = (BaseEdit) sender;
		editor.DoubleClick -= inplaceEditor_DoubleClick;
	}
}

VB.NET Sample

Private Sub gridView1_ShownEditor(sender As Object, e As EventArgs) Handles GridView1.ShownEditor
	Dim inplaceEditor As BaseEdit = DirectCast(sender, GridView).ActiveEditor
	AddHandler inplaceEditor.DoubleClick, AddressOf inplaceEditor_DoubleClick
End Sub

Private Sub inplaceEditor_DoubleClick(sender As Object, e As EventArgs)
	Dim editor As BaseEdit = DirectCast(sender, BaseEdit)
	Dim grid As GridControl = DirectCast(editor.Parent, GridControl)
	Dim pt As Point = grid.PointToClient(Control.MousePosition)
	Dim view As GridView = DirectCast(grid.FocusedView, GridView)
	Dim info As GridHitInfo = view.CalcHitInfo(pt)
	If (info.InRow OrElse info.InRowCell) AndAlso info.Column IsNot Nothing Then
		Dim cellValue As Object = view.GetRowCellValue(info.RowHandle, info.Column)
		MessageBox.Show(String.Format("Made a doubleclick at rowhandle: {0}, columnhandle: {1}, value: {2}.", info.RowHandle, info.Column.ColumnHandle, cellValue))
	End If
End Sub

Private Sub gridView1_HiddenEditor(sender As Object, e As EventArgs) Handles GridView1.HiddenEditor
	If sender.GetType() = GetType(BaseEdit) Then
		Dim editor As BaseEdit = DirectCast(sender, BaseEdit)
		RemoveHandler editor.DoubleClick, AddressOf inplaceEditor_DoubleClick
	End If
End Sub

OptionsBehavior.Editable set to True or False at Runtime

C# Sample

private void gridView1_DoubleClick(object sender, EventArgs e)
{
	GridView view = (GridView)sender;
	Point pt = view.GridControl.PointToClient(MousePosition);
	GridHitInfo info = view.CalcHitInfo(pt);
	if ((info.InRow || info.InRowCell) && info.Column != null)
	{
		DoSomething(view, info);
	}
}

private void DoSomething(GridView view, GridHitInfo info)
{
	Object cellValue = view.GetRowCellValue(info.RowHandle, info.Column);
	MessageBox.Show(string.Format("Made a doubleclick at rowhandle: {0}, columnhandle: {1}, value: {2}.", info.RowHandle, info.Column.ColumnHandle, cellValue));
}

private void gridView1_ShownEditor(object sender, EventArgs e)
{
	BaseEdit inplaceEditor = ((GridView)sender).ActiveEditor;
	inplaceEditor.DoubleClick += inplaceEditor_DoubleClick;
}

void inplaceEditor_DoubleClick(object sender, EventArgs e)
{
	BaseEdit editor = (BaseEdit)sender;
	GridControl grid = (GridControl)editor.Parent;
	Point pt = grid.PointToClient(Control.MousePosition);
	GridView view = (GridView)grid.FocusedView;
	GridHitInfo info = view.CalcHitInfo(pt);
	if ((info.InRow || info.InRowCell) && info.Column != null)
	{
		DoSomething(view, info);
	}
}

private void gridView1_HiddenEditor(object sender, EventArgs e)
{
	if (sender.GetType() == typeof (BaseEdit))
	{
		BaseEdit editor = (BaseEdit) sender;
		editor.DoubleClick -= inplaceEditor_DoubleClick;
	}
}

VB.NET Sample

Private Sub gridView1_DoubleClick(sender As Object, e As EventArgs)
	Dim view As GridView = DirectCast(sender, GridView)
	Dim pt As Point = view.GridControl.PointToClient(MousePosition)
	Dim info As GridHitInfo = view.CalcHitInfo(pt)
	If (info.InRow OrElse info.InRowCell) AndAlso info.Column IsNot Nothing Then
		DoSomething(view, info)
	End If
End Sub

Private Sub DoSomething(view As GridView, info As GridHitInfo)
	Dim cellValue As Object = view.GetRowCellValue(info.RowHandle, info.Column)
	MessageBox.Show(String.Format("Made a doubleclick at rowhandle: {0}, columnhandle: {1}, value: {2}.", info.RowHandle, info.Column.ColumnHandle, cellValue))
End Sub

Private Sub gridView1_ShownEditor(sender As Object, e As EventArgs)
	Dim inplaceEditor As BaseEdit = DirectCast(sender, GridView).ActiveEditor
	AddHandler inplaceEditor.DoubleClick, AddressOf inplaceEditor_DoubleClick
End Sub

Private Sub inplaceEditor_DoubleClick(sender As Object, e As EventArgs)
	Dim editor As BaseEdit = DirectCast(sender, BaseEdit)
	Dim grid As GridControl = DirectCast(editor.Parent, GridControl)
	Dim pt As Point = grid.PointToClient(Control.MousePosition)
	Dim view As GridView = DirectCast(grid.FocusedView, GridView)
	Dim info As GridHitInfo = view.CalcHitInfo(pt)
	If (info.InRow OrElse info.InRowCell) AndAlso info.Column IsNot Nothing Then
		DoSomething(view, info)
	End If
End Sub

Private Sub gridView1_HiddenEditor(sender As Object, e As EventArgs)
	If sender.GetType() = GetType(BaseEdit) Then
		Dim editor As BaseEdit = DirectCast(sender, BaseEdit)
		RemoveHandler editor.DoubleClick, AddressOf inplaceEditor_DoubleClick
	End If
End Sub

If you have any questions or suggestions feel free to rate this snippet, post a comment or Contact Us via Email.

Related links:

One thought on “How to use Doubleclick in Devexpress Gridview in C# and VB.NET”

Leave a Reply