Skip to main content

MouseClick Event On a Datagrid

  • May 29, 2013
  • 2 replies
  • 0 views

Hi,

I need to be able to "trap" a mouse click event on a DataGrid and I have found the following VB code:-

Private Sub DGV_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgvMyDatagrid.MouseClick

If sender.HitTest(e.X, e.Y).Type = DataGridViewHitTestType.RowHeader Then
    sender.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2
    sender.EndEdit()
Else
    sender.EditMode = DataGridViewEditMode.EditOnEnter
End If

End Sub

Or if you prefer the following C# Code:-

private void dataGridView1_MouseClick( object sender, MouseEventArgs e ) {
DataGridView.HitTestInfo hitInfo = this.dataGridView1.HitTest(e.X, e.Y);
if( hitInfo.Type == DataGridViewHitTestType.RowHeader ) {
    this.dataGridView1.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;
    this.dataGridView1.EndEdit();
}
else
{
  this.dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;
}
}

Which I have converted to the following Cobol Code:-

method-id. "dgvMain_MouseClick" final private.
01 ls-cHitTestInfo type "System.Windows.Forms.DataGridView HitTestInfo".
01 ls-x binary-long.
01 ls-y binary-long.
procedure division using by value sender as object e as type "System.Windows.Forms.MouseEventArgs".
    invoke dgvMain::"HitTest"(ls-x, ls-y) returning ls-cHitTestInfo

    if ls-cHitTestInfo::"Type" = type "System.Windows.Forms.DataGridViewHitTestType"::"RowHeader"
        set ls-x to ls-x
    else
        set ls-y to ls-y
    end-if
end method "dgvMain_MouseClick".

However its not working.

It builds and runs ok but it always goes down the "set ls-y to ls-y" path can anyone advise what I'm doing wrong?

Many thanks

Neil.

2 replies

Chris Glazier
Forum|alt.badge.img+2

Hi,

I need to be able to "trap" a mouse click event on a DataGrid and I have found the following VB code:-

Private Sub DGV_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgvMyDatagrid.MouseClick

If sender.HitTest(e.X, e.Y).Type = DataGridViewHitTestType.RowHeader Then
    sender.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2
    sender.EndEdit()
Else
    sender.EditMode = DataGridViewEditMode.EditOnEnter
End If

End Sub

Or if you prefer the following C# Code:-

private void dataGridView1_MouseClick( object sender, MouseEventArgs e ) {
DataGridView.HitTestInfo hitInfo = this.dataGridView1.HitTest(e.X, e.Y);
if( hitInfo.Type == DataGridViewHitTestType.RowHeader ) {
    this.dataGridView1.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;
    this.dataGridView1.EndEdit();
}
else
{
  this.dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;
}
}

Which I have converted to the following Cobol Code:-

method-id. "dgvMain_MouseClick" final private.
01 ls-cHitTestInfo type "System.Windows.Forms.DataGridView HitTestInfo".
01 ls-x binary-long.
01 ls-y binary-long.
procedure division using by value sender as object e as type "System.Windows.Forms.MouseEventArgs".
    invoke dgvMain::"HitTest"(ls-x, ls-y) returning ls-cHitTestInfo

    if ls-cHitTestInfo::"Type" = type "System.Windows.Forms.DataGridViewHitTestType"::"RowHeader"
        set ls-x to ls-x
    else
        set ls-y to ls-y
    end-if
end method "dgvMain_MouseClick".

However its not working.

It builds and runs ok but it always goes down the "set ls-y to ls-y" path can anyone advise what I'm doing wrong?

Many thanks

Neil.

The following works for me and takes the appropriate path depending on which component of the grid is clicked. If you set a breakpoint on the if statement and hover the mouse over ls-cHitTestInfo then you can see the Type that it is testing against.

      method-id dataGridView1_MouseClick final private.
      01 ls-cHitTestInfo type "System.Windows.Forms.DataGridView HitTestInfo".
      01 ls-x binary-long.
      01 ls-y binary-long.
      procedure division using by value sender as object e as type System.Windows.Forms.MouseEventArgs.

         set ls-cHitTestInfo to self::dataGridView1::HitTest(e::X, e::Y)
         if ls-cHitTestInfo::Type = type DataGridViewHitTestType::RowHeader
            set dataGridView1::EditMode to type DataGridViewEditMode::EditOnKeystrokeOrF2
            invoke self::dataGridView1::EndEdit
         else
            set self::dataGridView1::EditMode to type DataGridViewEditMode::EditOnEnter
         end-if

      end method.


Hi,

I need to be able to "trap" a mouse click event on a DataGrid and I have found the following VB code:-

Private Sub DGV_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgvMyDatagrid.MouseClick

If sender.HitTest(e.X, e.Y).Type = DataGridViewHitTestType.RowHeader Then
    sender.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2
    sender.EndEdit()
Else
    sender.EditMode = DataGridViewEditMode.EditOnEnter
End If

End Sub

Or if you prefer the following C# Code:-

private void dataGridView1_MouseClick( object sender, MouseEventArgs e ) {
DataGridView.HitTestInfo hitInfo = this.dataGridView1.HitTest(e.X, e.Y);
if( hitInfo.Type == DataGridViewHitTestType.RowHeader ) {
    this.dataGridView1.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;
    this.dataGridView1.EndEdit();
}
else
{
  this.dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;
}
}

Which I have converted to the following Cobol Code:-

method-id. "dgvMain_MouseClick" final private.
01 ls-cHitTestInfo type "System.Windows.Forms.DataGridView HitTestInfo".
01 ls-x binary-long.
01 ls-y binary-long.
procedure division using by value sender as object e as type "System.Windows.Forms.MouseEventArgs".
    invoke dgvMain::"HitTest"(ls-x, ls-y) returning ls-cHitTestInfo

    if ls-cHitTestInfo::"Type" = type "System.Windows.Forms.DataGridViewHitTestType"::"RowHeader"
        set ls-x to ls-x
    else
        set ls-y to ls-y
    end-if
end method "dgvMain_MouseClick".

However its not working.

It builds and runs ok but it always goes down the "set ls-y to ls-y" path can anyone advise what I'm doing wrong?

Many thanks

Neil.

Yep that works thanks Chris, is was this bit I missed (e::X, e::Y) DOH !