Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
DatagridViewCheckobxColumn Mysteries
Message
Information générale
Forum:
ASP.NET
Catégorie:
Formulaires
Versions des environnements
Environment:
VB 9.0
OS:
Windows Server 2012
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01600182
Message ID:
01600228
Vues:
36
>>>>I have a form with a dgv on it. The dgv has 1 column defined as a DataGridViewCheckBoxColumn. I would like this dgv to behave as a radiobutton group, i.e. when I click on one cell, that should be set to true, and the others to false.
>>>>
>>>>I've got it to work fine ... as long as the user does not click twice on the same cell. When that happens, none of the checkboxes are set. It seems that after I assign my the values, the current event is still executed (i.e. the current checkbox is changed into false).
>> >>
>>>>    Private Sub DataGridView1_CellContentClick(
>>>>        sender As Object, 
>>>>        e As DataGridViewCellEventArgs) 
>>>>        Handles DataGridView1.CellContentClick
>>>>        For Each r As DataGridViewRow In DataGridView1.Rows
>>>>            If r.Index = e.RowIndex Then
>>>>                r.Cells(0).Value = True
>>>>            Else
>>>>                r.Cells(0).Value = False
>>>>            End If
>>>>        Next
>>>>    End Sub
>>>>
>>>>
>>>>I would not mind assigning the values after the system has proccessed the event althogether, but I cannot find an event that gets triggered.
>>>>
>>>>The answer might be "hidden" in here
>>>>
>>>>http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcheckboxcolumn(v=vs.110).aspx
>>>>
>>>>but I cannot make any sense out of what they explain, so if anybody could translate this into plain English. :)
>>>>__quote__
>>>>Typically, check box cell values are intended either for storage, like any other data, or for performing bulk operations. If you want to respond immediately when users click a check box cell, you can handle the DataGridView.CellContentClick event, but this event occurs before the cell value is updated. If you need the new value at the time of the click, one option is to calculate what the expected value will be based on the current value. Another approach is to commit the change immediately, and handle the DataGridView.CellValueChanged event to respond to it. To commit the change when the cell is clicked, you must handle the DataGridView.CurrentCellDirtyStateChanged event. In the handler, if the current cell is a check box cell, call the DataGridView.CommitEdit method and pass in the Commit value.
>>>>__unquote__
>>>
>>>Seems like from the above you should be watching the CellContentClick event but the value at the point would be the old value. Since it's only a binary choice then the new value will be the opposite. i.e. if the value is false on CellContentClick you know it will become true and vice versa.
>>
>>Thanks. I tried all kinds of combinations, including the "alternative" where you commit the value. That works indeed, and the value you get in the currentCell.value is reliable, but the problem seems to be that you cannot reverse anything anymore.
>>
>>To put it in VFPese, I was looking for the Nodefault functionality, which would normally be available in the EditingControl.Validated event. And setting e.Cancel to True in BeginEdit is no alternative either, because the user gets stuck.
>>
>>In a "If you can't beat them, join them" kind of a mood if found out that you can set the CurrentCell to ReadOnly, preventing the user to click on the "set" cell.
>>
>>
>>    Private Sub DataGridView1_CellContentClick(
>>        sender As Object, 
>>        e As DataGridViewCellEventArgs) 
>>        Handles DataGridView1.CellContentClick
>>        For Each r As DataGridViewRow In DataGridView1.Rows
>>            If r.Index = e.RowIndex Then
>>                r.Cells(0).Value = True
>>                r.Cells(0).ReadOnly = True
>>            Else
>>                r.Cells(0).Value = False
>>                r.Cells(0).ReadOnly = False
>>            End If
>>        Next
>>    End Sub
>>
>
>Did you try the CellContentClick approach. Sounded like it should work ?

I did yes. In fact it's the one I am using, but the value in CurrentCell is not reliable, and even if it were, it does not change the fact that I cannot not have the CurrentCell updated. You see, if the user clicks on the "checked" cell, I would like to have the system not update, it should remain set, hence my nostalgy for nodefault.

In fact it works both ways, in CellContentClick, if you ask me, the CurrentCell is not reliably updated, in the alternative way, you do have a reliable value. The trouble is there's nothing you can do with it. I've tried CancelEdit; Invalidate, but nothing works and to the best of my knowledge, none of the ex post events fire.

Either I'm very wrong, but if you don't have the EditingControl (and you don't have that with dgvCheckboxColumn) you have no control on the value of the cell before control is returned.

If things have the tendency to go your way, do not worry. It won't last. Jules Renard.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform