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

Programmatically Setting Custom Control Cell values In Hierarchical Child Rows

3 Answers 152 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Frank
Top achievements
Rank 1
Frank asked on 21 Apr 2011, 05:09 PM
How can I programmatically change the checked/unchecked value in child row cells displayed as custom cells derived from RadCheckBoxElements? Here are details of my appliation:

I have an unbound Q1 2011 RadGridView control with a single hierarchical child level in a VB.Net 2010 Winforms application . There is a 1-to-many parent-child value relationship - ie a single parent row can have multiple child row values. When the RadGridView is expanded, the multiple child rows are made up of custom cells derived from a RadCheckBoxElement. I am using a custom cell derived from RadCheckboxElement rather than a GridViewCheckView column because I want to show a text value in the cell along with the checkbox.

Selecting a child row RadCheckBoxElement-derived cell updates a value in the parent row but also needs to uncheck any other checkboxes in the same child rows. My custom RadCheckBoxElement cell takes an EventHandler as a constructor parameter so the MouseUp event of the RadCheckBoxElement can call the parent form to update the desired parent row cell. I cannot figure out how to iterate through the other child row custom cells to uncheck any checked RadCheckBoxElements.

I have created my custom cell element class derived from GridDataCellElement. Here is the partial class definition:
Public Class CheckBoxCellElement
    Inherits GridDataCellElement
  
    Private chkBox As RadCheckBoxElement
    Private clickEvent As EventHandler
  
    Public Sub New(ByVal column As GridViewColumn, ByVal row As GridRowElement, ByVal extClickEvent As EventHandler)
        MyBase.New(column, row)
  
        clickEvent = extClickEvent
    End Sub

In the overridden CreateChildElements method I create the RadCheckboxElement and add a handler for the MouseUp event which in turn calls the passed in clickEvent. This clickEvent is passed in from the parent form in the RadGridView_CreateCell event thus:
Private Sub RadGridView1_CreateCell(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.GridViewCreateCellEventArgs) Handles RadGridView1.CreateCell
          
            If Not TypeOf e.Column Is CheckBoxColumn OrElse Not TypeOf e.Row Is GridDataRowElement Then Return
  
            e.CellElement = New CheckBoxCellElement(e.Column, e.Row, New EventHandler(AddressOf CheckBoxElementClicked))

Here is the method in the parent form that updates the desired RadGridView.ParentRow value - note that CheckBoxCellEventArgs is inherited from EventArgs with some extra fields:

Private Sub CheckBoxElementClicked(ByVal sender As Object, ByVal e As EventArgs)
        If Not TypeOf sender Is RadCheckBoxElement OrElse Not TypeOf e Is CheckBoxCellEventArgs Then Return
  
        Dim evArgs As CheckBoxCellEventArgs = CType(e, CheckBoxCellEventArgs)
  
        RadGridView1.MasterTemplate.Rows(evArgs.ParentRowIndex).Cells(_ciParentRowOptionValueColIndex).Value = evArgs.NewValue
    End Sub

I've tried finding a way to iterate through the custom cells containing RadCheckBoxElements in both the custom cell class RadCheckBoxElement_MouseUp event handler and in the parent form method passed in to the custom cell constructor as an EventHandler. Guidance would be most welcome.

Thanks,

Frank

3 Answers, 1 is accepted

Sort by
0
Frank
Top achievements
Rank 1
answered on 21 Apr 2011, 08:48 PM
I found a fix to my issue though I'm not sure it's the most elegant solution. I added a boolean property called Updating to my custom cell class. The custom cell class mouseup event handler sets this property to true and then triggers the parent form event handler to update the desired parent row field.

Here's where I am able to uncheck any checked RadCheckBoxElements in the child rows. In the RadGridView_CellFormatting event, I test for a cell element type of my custom CheckBoxCellElement and if I find it, check if its Updating property is true. If so I reset it to false. If Updating is false then I access the RadCheckBoxElement within the CheckBoxCellElement and set its ToggleState to Off.
Private Sub RadGridView1_CellFormatting(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.CellFormattingEventArgs) Handles RadGridView1.CellFormatting
        If Not TypeOf e.CellElement Is CheckBoxCellElement Then Return
  
        Dim chkBoxCell As CheckBoxCellElement = CType(e.CellElement, CheckBoxCellElement)
        If chkBoxCell.Updating Then
            chkBoxCell.Updating = False
        Else
            chkBoxCell.GetCheckBox.ToggleState = Telerik.WinControls.Enumerations.ToggleState.Off
        End If
    End Sub

There are still some issues I have to fix like changing the custom CheckBoxCellElement.RadCheckBoxElement.ToggleState programmatically from the hierarchical parent row. Any ideas?

Thanks

Frank
0
Frank
Top achievements
Rank 1
answered on 25 Apr 2011, 07:47 PM
0
Alexander
Telerik team
answered on 27 Apr 2011, 09:33 AM
Hello Frank,

I am glad to hear you have found the solution for your issues.

I would like to share a possible approach concerning your initial question for updating other custom cell when the value of a particular custom cell is updated. As you have written, you use the MouseUp event handler of your custom cell to indicate it is updating and then use this information to update the custom cells in other columns. You can use the event to update directly the value of the other cells as in the following dummy code:
Protected Overrides Sub OnMouseUp(ByVal e As System.Windows.Forms.MouseEventArgs)
    MyBase.OnMouseUp(e)
 
    Me.RowInfo.Cells("OtherCustomColumn").Value = ...
End Sub

Then you can use the SetContentCore method of the custom cell to synchronize the ToggleState of the other cell's check box element with its value:
Protected Overrides Sub SetContentCore(ByVal value As Object)
    MyBase.SetContentCore(value)
 
    ' set ToggleState to check box element
End Sub

I hope it helps.

Kind regards,
Alexander
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
GridView
Asked by
Frank
Top achievements
Rank 1
Answers by
Frank
Top achievements
Rank 1
Alexander
Telerik team
Share this question
or