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

Select all check box in header for gridview

4 Answers 348 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Sathish
Top achievements
Rank 1
Sathish asked on 18 Nov 2011, 11:03 AM
Hi Team,

I have a radgridview, iam binding all the data from the code behind which i will get from a wcfservice (No model class).

I have first column a check box which is a two way binding with the bool value from service.

I added another checkbox in header, now on check of the header checkbox all the rows should get selected.

How can i do this.

Please help

Thanks and Regards,
Sathish babu k.

4 Answers, 1 is accepted

Sort by
0
Nick
Telerik team
answered on 18 Nov 2011, 04:18 PM
Hello Sathish,

There are two ways you can do that, depending on whether you are trying to do it with MVVM or not.
You can subscribe for the Checked/Unchecked events on the CheckBox and do the selecting in the handlers.
It should look like: 

<CheckBox Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked"/>
   private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            foreach (var item in GridView.Items)
            {
                this.GridView.SelectedItems.Add(item);
            }
        }
 
        private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            this.GridView.SelectedItems.Clear();
        }


If you want to use MVVM you can bind the IsSelected property to a property in your VM.
I am attaching a sample project demonstrating the first approach.

Best wishes,
Nikolay Vasilev
the Telerik team

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

0
sathish
Top achievements
Rank 1
answered on 21 Nov 2011, 02:44 PM
Hi

Thanks for your reply,
I will explain you correctly what is my problem,
On the check event of the header checkbox all the check boxes(binded with bool value from the service) of the rows in the page should get selected and not the gridview items.
I dont have model class for the grid itemsource, directly binding from the service, Is there any way to acheive this.

Thanks and Regards,
Sathish babu k
0
Nick
Telerik team
answered on 22 Nov 2011, 09:23 AM
Hello Sathish,

You can use the GridViewSelectColumn. I am attaching an example so you can see exactly what I mean.
You have to set the SelectionMode to Extended for the grid in order to have the header checkbox.

<telerik:RadGridView AutoGenerateColumns="False" Name="GridView" ItemsSource="{Binding Items}"
                                 SelectedItem="{Binding Current, Mode=TwoWay}" SelectionMode="Extended">
This works whether you have bound data in models, or you are directly binding to a service as you said. 
Could you try it and see if it fits your requirements. 
Best wishes,
Nikolay Vasilev
the Telerik team

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

0
sathish
Top achievements
Rank 1
answered on 23 Nov 2011, 05:24 AM
Hi Nikolay,

I tried using the gridview select column, but the check boxes are not getting checked when i bind the bool value from the service. I saw in some other post that we cant bind to check property of the select column in the gridview.
see the below code,

this is the actual code which is used for the checkbox binidng,
<UserControl.Resources>
        <DataTemplate x:Key="boolDataTemplate"  >
            <CheckBox IsChecked="{Binding Select,Mode=TwoWay}"></CheckBox>
        </DataTemplate>
    </UserControl.Resources>
private void RadGridView_AutoGeneratingColumn(object sender, GridViewAutoGeneratingColumnEventArgs e)
        {
            if (e.Column.UniqueName.ToLower() == "select")
            {
                e.Column.CellTemplate = this.Resources["boolDataTemplate"] as DataTemplate;
            }
        }

In the save button click.
foreach (System.Linq.Dynamic.DynamicClass item in gvhierarchy.Items)
                {
                    if (Convert.ToBoolean(item.GetValue("Select")))
                    {
                        staff = new StaffModel();
                        staff.StaffID = Convert.ToInt32(item.GetValue("Staff_Seq_Number").ToString());
                        DestStaff.Add(staff);
                    }
                }
For this i have to give a select all option in the grid view.
Please suggest.

Thanks and Regards,
Sathish babu k
Tags
GridView
Asked by
Sathish
Top achievements
Rank 1
Answers by
Nick
Telerik team
sathish
Top achievements
Rank 1
Share this question
or