Select all check box in header for gridview

5 posts, 0 answers
  1. Sathish
    Sathish avatar
    1 posts
    Member since:
    Nov 2011

    Posted 18 Nov 2011 Link to this post

    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.
  2. Nick
    Admin
    Nick avatar
    593 posts

    Posted 18 Nov 2011 Link to this post

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

  3. sathish
    sathish avatar
    2 posts
    Member since:
    May 2011

    Posted 21 Nov 2011 Link to this post

    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
  4. Nick
    Admin
    Nick avatar
    593 posts

    Posted 22 Nov 2011 Link to this post

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

  5. sathish
    sathish avatar
    2 posts
    Member since:
    May 2011

    Posted 22 Nov 2011 Link to this post

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