Auto update CheckBox on CellClick

9 posts, 0 answers
  1. Dave
    Dave avatar
    4 posts
    Member since:
    Sep 2010

    Posted 23 May 2012 Link to this post

    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
  2. Dave
    Dave avatar
    4 posts
    Member since:
    Sep 2010

    Posted 24 May 2012 Link to this post

    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
  3. Svett
    Admin
    Svett avatar
    728 posts

    Posted 28 May 2012 Link to this post

    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 >>
  4. Dave
    Dave avatar
    4 posts
    Member since:
    Sep 2010

    Posted 28 May 2012 Link to this post

    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
  5. Svett
    Admin
    Svett avatar
    728 posts

    Posted 31 May 2012 Link to this post

    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 >>
  6. Evgenia
    Evgenia avatar
    1 posts
    Member since:
    Jun 2013

    Posted 16 Aug 2017 Link to this post

    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)
    }

  7. Hristo
    Admin
    Hristo avatar
    1510 posts

    Posted 16 Aug 2017 Link to this post

    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.
  8. Bao
    Bao avatar
    54 posts
    Member since:
    Jan 2015

    Posted 30 Aug 2017 in reply to Svett Link to this post

    Hi Svett,

    Why is it here ? 

    this.radGridView1.EndEdit();

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

  9. Hristo
    Admin
    Hristo avatar
    1510 posts

    Posted 31 Aug 2017 Link to this post

    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.
Back to Top