Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hi friends,

I have DataGridViewCheckBoxColumn cell in datagridview in my project.

I want the user to be able check DataGridViewCheckBoxColumn by clicking it when certain a condition is met.

When the condition is not met I expect the DataGridViewCheckBoxColumn to ignore the MouseClick_event.

I do not know the code I must place in the MouseClick_event handler to achieve this objective.

Any assistance would greatly appreciated.


Thanks in advance


Additional information copied from comment below
I thought there will be something like e.Handled as in KeyPress_Event.

For the purpose of clarity I have included my code

VB
If DataGridView1.CurrentCell.ColumnIndex = 9 Then
  'EventArgs.Empty = True
  Select Case e.Button
    Case (Windows.Forms.MouseButtons.Left) Or (Windows.Forms.MouseButtons.None) Or (Windows.Forms.MouseButtons.Middle)
    MessageBox.Show("Editing this column is not allowed for this transaction", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
    'EventArgs = Empty

    Case Windows.Forms.MouseButtons.Middle
    Case Windows.Forms.MouseButtons.Right
    Case Else
  End Select
End If
Posted
Updated 18-Jun-13 4:50am
v3

1 solution

For what you describe, the easiest way would be

C#
if condition != TRUE
  return

//here the rest of the code out of the if


or

C#
if condition == TRUE
{
  //your code here
}
else
  return



If that doesn't fit what you want to do... please use the "improve question" widget and explain it more detailed



OP's last comment with self-made workaround added here for other people
Thanks so much. I have found a handle to my problem. I just put the code(messagebox and making the cell readonly)to execute when the condition is not met. Complete routine is shown below:

VB
Private Sub DataGridView1_MouseClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseClick
  If (DataGridView1.CurrentCell.ColumnIndex = 12) Then
    If (DataGridView1.CurrentRow.Cells("TransType").FormattedValue.ToString.ToUpper <> "E") AndAlso (DataGridView1.CurrentRow.Cells("TransType").FormattedValue.ToString.ToUpper <> "D") Then
      'EventArgs.Empty = True
      Select Case e.Button
        Case Windows.Forms.MouseButtons.Left
          DataGridView1.CurrentCell.ReadOnly = True
          MessageBox.Show("Editing this column is not allowed for this transaction", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
          'EventArgs = Empty
        Case Windows.Forms.MouseButtons.Middle
          DataGridView1.CurrentCell.ReadOnly = True
          MessageBox.Show("Editing this column is not allowed for this transaction", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
          'EventArgs = Empty
        Case Windows.Forms.MouseButtons.Right
          DataGridView1.CurrentCell.ReadOnly = True
          MessageBox.Show("Editing this column is not allowed for this transaction", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
          'EventArgs = Empty 
        Case Else
          DataGridView1.CurrentCell.ReadOnly = False
      End Select
    Else
      DataGridView1.CurrentCell.ReadOnly = False
    End If
  End If
End Sub


Thanks so much for your assistance
 
Share this answer
 
v2
Comments
noblepaulaziz 17-Jun-13 11:11am    
Thank you Nelek. My problem is not how to code condition but the code to ignore the click event
Nelek 18-Jun-13 4:31am    
If you get into the event and the first thing you do is an "empty" return, when the condition is not matched, is almost the same than ignoring the click-event but much easier. You are not ignoring the click of the mouse, you are just executing nothing when you don't want to.
noblepaulaziz 18-Jun-13 10:29am    
Thank you for your response. That is exactly what I mean. I thought there will be something like e.Handled as in KeyPress_Event.

For the purpose of clarity I have included my code

If DataGridView1.CurrentCell.ColumnIndex = 9 Then
'EventArgs.Empty = True
Select Case e.Button
Case (Windows.Forms.MouseButtons.Left) Or (Windows.Forms.MouseButtons.None) Or (Windows.Forms.MouseButtons.Middle)
MessageBox.Show("Editing this column is not allowed for this transaction", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
'EventArgs = Empty

Case Windows.Forms.MouseButtons.Middle
Case Windows.Forms.MouseButtons.Right
Case Else
End Select
End If
Nelek 18-Jun-13 10:40am    
You are welcome. I am curious as well, if what you meant is possible or not, but I am not the right one to say it (not a big programmer in VB). I just saw it on the easy way.

On the other hand... I would like to know... Is what I suggested solving your problem?
noblepaulaziz 18-Jun-13 12:46pm    
No, both EventArgs.Empty = True and EventArgs.Empty raise compiler error

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900