Click here to Skip to main content
15,889,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm currently working on a windows application, using a datagridview. This is my first application using a datagridview and I'm having issues with how the event handlers work with the different types of objects available for each column. For example, the first column I'm using a check-box, and if this column is checked I would like to disable the use of another column. If the check-box is not checked, then I would enable the column.

Any assistance would be appreciated, thanks
Posted

There is a ContentClick event for the DataGridView. Handle that event and check if the checkbox was checked or not. If yes, freeze the row. See if this helps:

C#
void DataGridViewCellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            (sender as DataGridView).Rows[e.RowIndex].Frozen = true;
        }
 
Share this answer
 
Thank you d@nish, I will try this event handler on my form. I'm beginning to see how powerful the datagridview really is compared to your standard objects, writing simple code such as "enabled = true" or "enabled = false". Once I have this event working properly for my checkboxcolumn, I can look into working with my other events. It appears to me, this "CellContentClick" event is going to be one of the more common used events for the datagridview.
 
Share this answer
 
Comments
dan!sh 24-Aug-11 14:04pm    
Two points:
1. I have not checked for the checked status. I left it for you to do.
2. Instead of replying to your question, use "Have a Question or Comment?" link below others replies so that they are notified. If you want to propose a solution, update your question with the answer too.

Good luck.
GoBrvs01 24-Aug-11 14:23pm    
Thanks, I was wondering if I was responding back in the correct manor. I was doing some testing with other event args and have also came up with the following:

Private Sub grdvwRegister_CellValueChanged(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _
Handles grdvwRegister.CellValueChanged
If IsCheckboxCell(e) Then
Call ActivateComboBoxes(sender, e)
End If
End Sub


Private Sub ActivateComboBoxes(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs)

Dim chkbox As New DataGridViewCheckBoxCell(False)
chkbox = Me.grdvwRegister.CurrentRow.Cells(0)
If chkbox.Value = chkbox.TrueValue Then
Me.Label1.Text = "True"
Me.grdvwRegister.CurrentRow.Cells(1).ReadOnly = False
Me.grdvwRegister.CurrentRow.Cells(2).ReadOnly = False
Me.grdvwRegister.CurrentRow.Cells(3).ReadOnly = True
If Me.grdvwRegister.CurrentRow.Cells(3).Value > -1 Then
Me.grdvwRegister.CurrentRow.Cells(3).Value = -1
End If
Else
Me.Label1.Text = "False"
Me.grdvwRegister.CurrentRow.Cells(1).ReadOnly = True
Me.grdvwRegister.CurrentRow.Cells(2).ReadOnly = True
Me.grdvwRegister.CurrentRow.Cells(3).ReadOnly = False
End If
End Sub


The label is only there for testing, seeing if I actually am triggering the checkboxcolumn change and writing some text to the label. I don't really like using the 'cellvaluechanged' event, because this event won't trigger until the user leaves the checkboxcolumn. I'm going to try your suggestion again, using the 'cellcontentclick' event. I had to use 'readonly', rather than 'frozen' but will continue to look at other options. Thanks for your help, will let you know any new progress.......

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