This is a migrated thread and some comments may be shown as answers.

Auto update CheckBox on CellClick

8 Answers 632 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Dave
Top achievements
Rank 1
Dave asked on 23 May 2012, 03:13 PM
Hi Support,
I have an IBindingList of objects with a boolean property that a checkbox column is bound to. Only one object in the collection can have bolSelected = true at any time which is controlled by the collection so that when bolSelected is set to true, all other items will automatically have bolSelected = false. What I am hoping to see in the grid is when the checkbox is clicked, bolSelected is set to true and other selected item in the grid has their checkboxes unchecked automatically (as the bolSelected = false for all other objects now).

Is there was a way to make the CheckBox click update the row immediately, without entering into edit mode (as can be seen by the changing of icon in the row header). I can't seem to get the CheckBox to fire the changed event, which means that the change to bolSelected isn't being propagating to the rest of the collection. What is then the best way to refresh the grid now that the collection has changed?

Thanks for your help,
Dave

8 Answers, 1 is accepted

Sort by
0
Dave
Top achievements
Rank 1
answered on 24 May 2012, 05:46 PM
Hi Support,

Well, I managed to code around it. Instead of using a GridViewCheckBoxColumn I used a GridViewImageColumn. In the ViewCellFormatting event I set the image for the row to a checked checkbox image (if bolSelected is True) or to an unchecked checkbox image (if bolSelected is False). The CellClick event fires OK so I use it to manually toggle the bolSelected flag. Then I force a row refresh which updates the new image. The code is below.

Private Sub gvScoring_CellClick(sender As Object, e As Telerik.WinControls.UI.GridViewCellEventArgs) Handles gvScoring.CellClick
    If e.Column.Name = "selected" Then
        Dim objScoring As clsScoring = e.Row.DataBoundItem
        If objScoring IsNot Nothing Then
            objScoring.bolSelected = Not objScoring.bolSelected
            For Each row As GridViewRowInfo In gvScoring.Rows
                row.InvalidateRow()
            Next
        End If
    End If
End Sub
 
Private Sub gvScoring_ViewCellFormatting(sender As Object, e As Telerik.WinControls.UI.CellFormattingEventArgs) Handles gvScoring.ViewCellFormatting
    If e.Column.Name = "selected" Then
        Dim objScoring As clsScoring = e.Row.DataBoundItem
        If objScoring Is Nothing OrElse Not objScoring.bolSelected Then
            e.CellElement.Image = GetSmallImage("checkbox_unchecked")
        ElseIf objScoring.bolSelected Then
            e.CellElement.Image = GetSmallImage("checkbox_checked")
        End If
    End If
End Sub

There's no checkbox involved so there is no hassle with forcing an update. Obviously having the checkbox column with some sort of auto row update machanism is a cleaner solution, but it seems to do the trick... and it was driving me crazy.

Thanks,
Dave
0
Svett
Telerik team
answered on 28 May 2012, 10:46 AM
Hello David,

Note that in your approach you do not commit the changes to the underlying data source. I recommend using the following approach:

this.radGridView1.ValueChanged += new EventHandler(radGridView1_ValueChanged);

private void radGridView1_ValueChanged(object sender, EventArgs e)
{
    RadCheckBoxEditor gridEditor = sender as RadCheckBoxEditor;
 
    if (gridEditor != null)
    {
        this.radGridView1.EndEdit();
 
        foreach (GridViewRowInfo row in this.radGridView1.ChildRows)
        {
            if (row != this.radGridView1.CurrentRow)
            {
                row.Cells[this.radGridView1.CurrentColumn.Name].Value = false;
            }
        }
    }
}

I hope this helps.

Greetings,
Svett
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
0
Dave
Top achievements
Rank 1
answered on 29 May 2012, 03:08 AM
Hi Svett,

Thanks for the reply. In the posted code the underlying data object is manipulated directly:

Private Sub gvScoring_CellClick(sender As Object, e As Telerik.WinControls.UI.GridViewCellEventArgs) Handles gvScoring.CellClick
    If e.Column.Name = "selected" Then
>>      Dim objScoring As clsScoring = e.Row.DataBoundItem        <<
        If objScoring IsNot Nothing Then
>>          objScoring.bolSelected = Not objScoring.bolSelected  <<
            For Each row As GridViewRowInfo In gvScoring.Rows
                row.InvalidateRow()
            Next
        End If
    End If
End Sub

This object is in a collection (and this collection is the DataSource for the grid). When the bolSelected property is set to true the collection will reset the bolSelected flag for any other items. That leaves me with an updated data set but with a grid that is potentially now out-of-sync. To rectify this I then invalidate the rows. There is probably a better way to refresh the other rows, but the InvalidateRow seems to work.

But, the reason for my original post was to find out if there is any way to make the CheckBox column immediately update the data object on the cell click. Any ideas there?

Thanks,
Dave
0
Svett
Telerik team
answered on 31 May 2012, 01:27 PM
Hello David,

There is no other way to achieve immediate committing of the changes. It is up to you to use your or mine approach to implement the desired behavior.

Kind regards,
Svett
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
0
Evgenia
Top achievements
Rank 1
answered on 16 Aug 2017, 07:51 AM

Was looking for something similar and found that

private void RadGridView1_ValueChanged(object sender, EventArgs e)
{
            var index = this.radGridView1.CurrentCell.RowIndex;  // The index of the row
            var isChecked = this.radGridView1.ActiveEditor.Value; // The value of the checkbox - on / off (ToggleState)
}

0
Hristo
Telerik team
answered on 16 Aug 2017, 09:44 AM
Hi Evgenia,

Thank you for updating the thread. The provided code snippet is valid in the context of a check box.

Please let me know if you need further assistance.

Regards,
Hristo
Progress Telerik
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Bao
Top achievements
Rank 1
answered on 31 Aug 2017, 12:56 AM

Hi Svett,

Why is it here ? 

this.radGridView1.EndEdit();

I still do not understand the usage of this line in this function.

0
Hristo
Telerik team
answered on 31 Aug 2017, 03:19 PM
Hello Bao,

The EndEdit method will force the grid to leave edit mode and to submit the value of the editor to the cell. Not calling the method would update the cell where the click has been performed on validation. Now this behavior can be controlled by setting the GridViewCheckBoxColumn.EditMode property to OnValueChangehttp://docs.telerik.com/devtools/winforms/gridview/columns/column-types/gridviewcheckboxcolumn.

This property is not available in the 2012 version of the controls as it was introduced in R3 2014.

I hope this helps. Should you have further questions please do not hesitate to write back.

Regards,
Hristo
Progress Telerik
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
GridView
Asked by
Dave
Top achievements
Rank 1
Answers by
Dave
Top achievements
Rank 1
Svett
Telerik team
Evgenia
Top achievements
Rank 1
Hristo
Telerik team
Bao
Top achievements
Rank 1
Share this question
or