Click here to Skip to main content
15,886,101 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Respected members,

I have one simple question. I have search a lot but I don't know why I am not getting correct answer.

my question is, I am having one datagridview in my vb.net windows form. There is checkboxcolumn at the last column (column no 8).I want when user check the checkbox then column no 7's value should transfer into column no 6 and column no 7 value should become 0.

for this I have used :


now Please help me to get my desired output.

thanks in advance.

hope I get my answer soon.

What I have tried:

Option 1 :

VB
If DirectCast(grdInvoices.Rows(e.RowIndex).findcontrol("chkPaid"), checkBox).Checked = True Then
               grdInvoices.Rows(e.RowIndex).Cells(6).Value = grdInvoices.Rows(e.RowIndex).Cells(7).Value
               grdInvoices.Rows(e.RowIndex).Cells(7).Value = 0
           End If


now error is : findcontrol is not member of datagrid view

Option 2 :

If CBool(DirectCast(grdInvoices.Rows(e.RowIndex).Cells("chkPaid").Value, DataGridViewCheckBoxCell).Value) = True Then
grdInvoices.Rows(e.RowIndex).Cells(6).Value = grdInvoices.Rows(e.RowIndex).Cells(7).Value
grdInvoices.Rows(e.RowIndex).Cells(7).Value = 0
End If

now error is : its always fired when I lost focus of checkbox cell after unselect of checkbox
Posted
Updated 7-Jun-16 6:54am
Comments
Sergey Alexandrovich Kryukov 7-Jun-16 12:34pm    
The whole idea of FindControl would be wrong. Of course, there is no such thing in System.Windows.Forms.
Your use of hard-coded names like "chkPaid" is also wrong; it leads to totally unmaintainable code.
The solution of your problem is different, I'll explain...
—SA

1 solution

Please see my comment to the question. You have some serious problems with your code you will need to fix.

Now, capturing of the editing events of the editing control embedded in a cell can be difficult, but DataGridView provides all hooks for doing so. In your case, the problem can be solved in some simper ways. First of all, you can simply handle the event System.Windows.Forms.DataGridView.CellEndEdit:
DataGridView.CellEndEdit Event (System.Windows.Forms).

In the handler method, you can check up the value of the cell: DataGridViewCell.Value Property (System.Windows.Forms).

You case is very simple: if the editor is the check box, it means that the type of the value is Boolean, so you can bravely type-case the property DataGridViewCell.Value value to Boolean and act depending of the value.

If, by some reason, you need to know the combination of the values before and after cell value modification, you can also handle the event DataGridView.CellBeginEdit:
DataGridView.CellBeginEdit Event (System.Windows.Forms).

In the simple case of check box cells, you always change from False to True or from True to False, but the user also can leave the value unchanged.

In more complicated cases, you may also need to use the events DataGridView.EditingControlShowing, DataGridView.EditModeChanged, property DataGridView.EditingControl, and so on. Please review all the DataGridView members related to editing; they are very well described in MSDN:
DataGridView Class (System.Windows.Forms).

One big warning to everyone: the editing control of the same is reused by DataGridView. You have to make sure that you add event handler to the control instance only once. You have to check up that the events handles are already added to the invocation list of the event instances of the same instance of the control and then not add the handlers again. Failure to check it up will result in a memory leak. This is one of the relatively rare cases of the memory leak possibilities in managed memory.

—SA
 
Share this answer
 
Comments
Mogha Ritesh 8-Jun-16 5:26am    
Thanks for response. let me try according your guidance.
Sergey Alexandrovich Kryukov 8-Jun-16 9:41am    
Will you accept the answer formally?
—SA

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