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

Select All in GridViewCheckBoxColumn

11 Answers 1913 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Tarun
Top achievements
Rank 1
Tarun asked on 16 Aug 2011, 04:58 PM
I have a WPF radgridview with the first column being GridViewCheckBoxColumn. I have templated the header

        <telerik:RadGridView x:Name="WiresGV" AutoGenerateColumns="False" Grid.Row="1" ItemsSource="{Binding Wires}" ShowGroupPanel="False" ShowInsertRow="True" AlternateRowBackground="OliveDrab" clr:XWireAppCommands.DataGridDoubleClickCommand="{Binding TarunCommand}">
            <telerik:RadGridView.Columns>
                <telerik:GridViewCheckBoxColumn DataMemberBinding="{Binding IsSelected}" Name="CBCol" AutoSelectOnEdit="True" IsFilterable="False" Width="60">
                    <telerik:GridViewCheckBoxColumn.Header>
                                <CheckBox Content="Select" Name="HeaderItem" Click="Button6_Click" Foreground="White"></CheckBox>
                    </telerik:GridViewCheckBoxColumn.Header>
                </telerik:GridViewCheckBoxColumn>
                <telerik:GridViewDataColumn DataMemberBinding="{Binding WireDate}" Header="Date" DataFormatString=" {0:dd, MMM, yyyy}" />
 
 
.....
When the user click on the checkbox in the header i want to udpate all checkboxes in the rows to checked or unchecked. I am trying to do that with this code by "cb is always nothing". Pls advise
For Each row As GridViewRow In Me.WiresGV.ChildrenOfType(Of GridView.GridViewRow)()
 
    If (TypeOf row Is GridViewNewRow) Then
        Continue For
    End If
 
    Dim cb As CheckBox
    cb = row.Cells(0).ChildrenOfType(Of CheckBox)().FirstOrDefault
      cb.IsChecked = True
 
 
Next

11 Answers, 1 is accepted

Sort by
0
Vanya Pavlova
Telerik team
answered on 16 Aug 2011, 05:46 PM
Hello Tarun,

 

You may use the built-in GridViewSelectColumn which provides this functionality out-of-the-box. Since you have set the SelectionMode property either to Multiple or Extended you will be able to use the CheckBox control within the Header and the ability to select all items in RadGridView.


Regards,
Vanya Pavlova
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

0
Tarun
Top achievements
Rank 1
answered on 16 Aug 2011, 06:06 PM
Vanya, can i data bind it to a property ? i also like the AutoSelectOnEdit=True property of the GridViewCheckBoxColumn. Pls advise
0
Vanya Pavlova
Telerik team
answered on 16 Aug 2011, 06:59 PM
Hello Tarun,

 

GridViewSelectColumnis internally bound to the GridViewRow'sIsSelected property. By that reason it cannot be related to another custom property of your ViewModel.
You may define the GridViewCheckBoxColumn's Header, bind it to the property you want and predefine its Header in an appropriate manner, refer to the following:


<telerik:GridViewCheckBoxColumn DataMemberBinding="{Binding IsPlaying}">
    <telerik:GridViewCheckBoxColumn.Header>
        <CheckBox telerik:StyleManager.Theme="Office_Black" Click="CheckBox_Click" />
    </telerik:GridViewCheckBoxColumn.Header>
</telerik:GridViewCheckBoxColumn>


Private Sub CheckBox_Click(sender As Object, e As System.Windows.RoutedEventArgs)
    Dim checkBox = TryCast(sender, CheckBox)
    If checkBox IsNot Nothing Then
        If checkBox.IsChecked.GetValueOrDefault() Then
            Me.playersGrid.SelectAll()
        Else
            Me.playersGrid.UnselectAll()
        End If
    End If
End Sub
Greetings,
Vanya Pavlova
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

0
Tarun
Top achievements
Rank 1
answered on 16 Aug 2011, 07:27 PM
Vanya, The code does not do anything for me. Not sure why. I dont follow the logic either.

From my understandingMe.playersGrid.SelectAll() will select all rows in the datagrid. Instead , i want to check the checkbox column.  Pls advise

0
Vanya Pavlova
Telerik team
answered on 16 Aug 2011, 07:47 PM
Hello Tarun,

 
The solution below demonstrates how to implement the GridViewSelectColumn using standard GridViewCheckBoxColumn. As I can understand you want to select the cells in that particular column when you click on the header. Keep in mind that you may select rows when the SelectionMode is set to FullRow and to select cells when the SelectionMode property of RadGridView is set to Cell. You may use extension method ChildrenOfType and to find the cells in a particular column and set its IsSelected property to True.


Regards,
Vanya Pavlova
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

0
Tarun
Top achievements
Rank 1
answered on 16 Aug 2011, 11:20 PM
Vanya,

I am trying the following code but cb is always nothing. Can you advise why that is happening. I am not able to find documentation for row.cells ...

For Each row As GridViewRow In Me.WiresGV.ChildrenOfType(Of GridView.GridViewRow)()
 
    If (TypeOf row Is GridViewNewRow) Then
        Continue For
    End If
 
    Dim cb As CheckBox
    cb = row.Cells(0).ChildrenOfType(Of CheckBox)().FirstOrDefault
      cb.IsChecked = True
 
 
Next

0
Vanya Pavlova
Telerik team
answered on 17 Aug 2011, 09:45 AM
Hello Tarun,

 
You should use the following as a pattern:


<telerik:RadGridView x:Name="gridView" ItemsSource="{Binding Collection}" SelectionUnit="Cell" AutoGenerateColumns="False">
      <telerik:RadGridView.Columns>
          <telerik:GridViewCheckBoxColumn Width="100"  DataMemberBinding="{Binding Property2}">
              <telerik:GridViewCheckBoxColumn.Header>
                  <CheckBox telerik:StyleManager.Theme="Office_Black" Checked="CheckBox_Checked"/>
                  </telerik:GridViewCheckBoxColumn.Header>
              </telerik:GridViewCheckBoxColumn>
          </telerik:RadGridView.Columns>
      </telerik:RadGridView>
  



Private Sub CheckBox_Checked(sender As Object, e As RoutedEventArgs)
    Dim checkBox = DirectCast(sender, CheckBox)
    If checkBox IsNot Nothing Then
        If checkBox.IsChecked = True Then
            Dim grid = checkBox.ParentOfType(Of RadGridView)()
            Dim cells = grid.ChildrenOfType(Of GridViewCell)().Where(Function(f) f.Column.DisplayIndex = 0).ToList()
            For Each cell As var In cells
 
                cell.IsSelected = True
            Next
 
        End If
    End If
 
End Sub
     

You may change this snippet in the way you need. You may read more about Selection in RadGridView in our online docs, please follow this link. 


Regards,
Vanya Pavlova
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

0
Tarun
Top achievements
Rank 1
answered on 17 Aug 2011, 03:03 PM
Vanya, your code selects all the cells of my GridViewCheckBoxColumn. However, what i want it to do is the select the checkbox within those cells. Pls advise. The code i posted in my first post is something i found on this forum as a recommendation from the Telerik Team for this situation but it does not work as my variable cb is always nothing.
0
Pavel Pavlov
Telerik team
answered on 17 Aug 2011, 03:20 PM
Hello Tarun,

The best approach here would be not to iterate the UI elements. It is better to iterate trough your business objects and set their "IsSelected" property to true .
Since checkboxes are bound to this property, the will automatically update  to the proper state.


In other words instead of finding the checkboxes, just make a "foreach" in the items used as ItemsSource for the gird and set their property.

* in order this to have effect ,your objects need to implement the INotifyPropertyChanged Interface. This will allow RadGridView to sense the change and update the UI with the new checked states "

If all this sounds obscure, just paste me the implementation of your business object and I will gather a small sample for you .

Greetings,
Pavel Pavlov
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

0
Tarun
Top achievements
Rank 1
answered on 17 Aug 2011, 03:42 PM
Vanya, i modified my solution to iterate through business objects and it works. I was just trying this gridview approach first just so i can lean it. While i have figured out the solution, can you still advise on why my cb is always nothing. i want to understand.
0
Pavel Pavlov
Telerik team
answered on 17 Aug 2011, 03:47 PM
Hi Tarun,

We can debug this for you but we will need a runnable repro to step through it and see what and where went wrong.
Having only fragments of the code , I can just guess what may be the cause for the trouble.

All the best,
Pavel Pavlov
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

Tags
GridView
Asked by
Tarun
Top achievements
Rank 1
Answers by
Vanya Pavlova
Telerik team
Tarun
Top achievements
Rank 1
Pavel Pavlov
Telerik team
Share this question
or