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

Working with Checkboxes in CellTemplates

3 Answers 32 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Louis
Top achievements
Rank 1
Louis asked on 29 Mar 2011, 06:14 PM
Greetings,

      I have a unique situation where I have a grid that needs a check box that is used for a separate function that does something outside the grid (just need to pass it information from the selected rows).   Also, selecting a grid row sets up data on another grid.   I tried using the GridViewCheckBoxColumn.  However, I found selecting a row also selects or deselcts the checkbox.  Not the interface I was looking for.  To get around this and the separate the two , I substituted this column type for a custom column type as follows:

<telerik:GridViewColumn Header="Request a Copy" HeaderTextAlignment="Center" TextAlignment="Center" UniqueName="colReqInvc" >
                        <telerik:GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <CheckBox x:Name="RCINvcChkBox" IsChecked="False" HorizontalAlignment="Center" DataContext="{Binding Path=SelectedItem, Mode=TwoWay}" />
                            </DataTemplate>
                        </telerik:GridViewColumn.CellTemplate>
                    </telerik:GridViewColumn>

I have the grid bound to a custom class defined as follows:
Public Class ViewAllInvoice
    Implements INotifyPropertyChanged
  
    Private _selectedItem As Boolean
  
    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
  
    Public Property TranID As String
    Public Property TranDate As String
    Public Property TranAmt As Decimal
    Public Property Balance As Decimal
    Public Property DueDate As String
    Public Property DiscAmt As Decimal
    Public Property PmtTermsID As String
    Public Property InvcCmnt As String
    Public Property InvcKey As Int32
  
    'Property that will hold the value of checkboxes on the grid
    'and also wired up to detected the property changed.
    Public Property SelectedItem As Boolean
        Get
            Return _selectedItem
        End Get
        Set(ByVal value As Boolean)
            _selectedItem = value
            OnPropertyChanged(New PropertyChangedEventArgs("SelectedItem"))
        End Set
    End Property
  
    Public Sub OnPropertyChanged(ByVal e As PropertyChangedEventArgs)
        If PropertyChangedEvent IsNot Nothing Then
            RaiseEvent PropertyChanged(Me, e)
        End If
    End Sub
      
End Class

The grid seems to work fine, but I need to do two things with it when trying to make a call to the separate function.  The first is to get a count of all the checkboxes  that are checked.  The second is to only retrieve the data from the rows that do have  their checkboxes checked.   What would be the correct way of doing this as I am using a cell template in this scenario?

Thanks

3 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 30 Mar 2011, 06:47 AM
Hello,

 In my opinion you need to bind (two way) IsChecked to some custom property in your data item - otherwise when you scroll the grid up and down you will loose previously checked rows (RadGridView is virtualized). When you bind the checkbox to a property of your data item you can easily check later which data items are checked.

Best wishes,
Vlad
the Telerik team
0
Louis
Top achievements
Rank 1
answered on 30 Mar 2011, 02:39 PM
It is bound two way to the property SelectedItem in the class as the code previously shown indictaes.   When I manually selected each checkbox and scroll up and down, the value remains in place.  However, using code that was posted to this forum to use the column header to select or deselect all the checkboxes in the grid, it seems that only works for visible rows and if you scroll it deselects some but not all rows that scroll off the visible grid area.    Its as if setting the checkbox in code doesn't fire off the changed events in the bound class.

Also, what is the technique for getting a count of rows that are checked as was asked for originally since I'm using a cell template with a checkbox and not a checkbox column?
0
Louis
Top achievements
Rank 1
answered on 31 Mar 2011, 10:14 PM
Well digging into this further,  it looks like even with two way enabled, the checkbox is not setting the underlying value in the data object it is bound to.  Stepping through the code and drilling down into the row I can see this, though the checkbox is checked.  Not sure why that is as everything appears correct.  For now the following code is working for me as far as checking what rows are checked and to deal with those that are:

For Each Item As Object In dgInvoices.Items
           Dim row As GridViewRow = TryCast(dgInvoices.ItemContainerGenerator.ContainerFromItem(Item), GridViewRow)
           If row IsNot Nothing Then
               If row.Cells(0).Content.IsChecked Then
                   Dim rowInfo As ViewAllInvoice = row.DataContext
                   SelectedCount += 1
                   'Execute the process to request the invoice
                   _theContext.RequestPortalInvoice(0, 0, theDate, _App.UserAccountKey, rowInfo.InvcKey)
                   InvcList.AppendLine(rowInfo.TranID)
                   'Clear Check box
                   row.Cells(0).Content.IsChecked = False
               End If
           End If
       Next

 However, it would be nice to know why the checkbox isn't updating the underlying property in the class it is bound..
Tags
GridView
Asked by
Louis
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Louis
Top achievements
Rank 1
Share this question
or