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

CheckBox in Grouped Column

8 Answers 199 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mike
Top achievements
Rank 1
Mike asked on 28 Oct 2008, 03:18 PM

I was wondering if you can put a checkbox in the row of a grouped column. This way if I check that checkbox all of the rows in that grouping would get checked, I look forward to your answer. 

P.S. I don't want to nest queries to achieve this, just one query.

8 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 29 Oct 2008, 04:28 AM
Hi Mike,

Check out the following code library submission which demonstrates how to select/deselect all rows in a group by means of a checkbox residing in each group header.
Selecting all items in a group with a checkbox in the group header item

Regards
Shinu
0
Rod
Top achievements
Rank 1
answered on 30 Oct 2008, 04:54 PM
Has anyone found a way to get around the limitation mentioned in this article (group header text disappearing when check box is rendered)?

I really need this functionality to work properly. I'm surprised this isn't something that's built into the RadGrid.

Please help. I'm running out time to make this work and might have to scrap my original design if I can't get this to work.

Thanks,

Rod
0
Bodevain Svensson
Top achievements
Rank 1
answered on 31 Oct 2008, 07:34 AM
How about extracting the text value from the group header using the DataCell.Text property before adding the checkbox control? I suppose that adding the text value in another LiteralControl or Label to the cell controls collection should do the job.

Bodevain
0
Rod
Top achievements
Rank 1
answered on 31 Oct 2008, 04:19 PM
Hi Bodevain,

Thanks for the reply.
That is one of the things I tried:
On ItemDataBound I added the DataCell.Text value as an item attribute then on ItemCreated (where it gets overwritten), I'd check to see if the attribute was blank and add it back to the check box text property.

I really don't understand why that didn't work.
If that's not what you meant, would you be able to post an example?

Thanks,

Rod
0
Bodevain Svensson
Top achievements
Rank 1
answered on 03 Nov 2008, 07:39 AM
You have to span your logic in the ItemCreated and ItemDataBound event at the same time because the controls are added on ItemCreated while the data for them is set on ItemDataBound. Therefore if you add the text value on ItemCreated only, it will be erased when the ItemDataBound event is fired. Check out this topic.

I can not provide a direct example because the process is rather custom and relies on your internal logic - I hope this explanation will put you on the right track.

Bodevain
0
antoniodlp
Top achievements
Rank 2
answered on 02 Feb 2009, 12:01 AM
This is how I solved the problem of having databound group row text in the 'Selecting all items in a group with a checkbox in the group header item' example.

The solution is to set the CheckBox text in the ItemDataBound event but not in the ItemCreated event.

        static void check_CheckedChanged(object sender, EventArgs e) 
        { 
            CheckBox check = (CheckBox)sender; 
            GridGroupHeaderItem groupHeader = (GridGroupHeaderItem)check.NamingContainer; 
            GridItem[] children = groupHeader.GetChildItems(); 
            foreach (GridItem child in children) 
            { 
                child.Selected = check.Checked; 
            } 
        } 
 
        protected void RadGrid1_ItemCreated(object source, GridItemEventArgs e) 
        { 
            if (e.Item is GridGroupHeaderItem) 
            { 
                var item = e.Item as GridGroupHeaderItem; 
                var check = new CheckBox(); 
                check.AutoPostBack = true
                check.ID = "CheckAll"
                check.CheckedChanged += check_CheckedChanged; 
                item.DataCell.Controls.Add(check); 
            }  
        } 
 
        protected void RadGrid1_ItemDataBound(object source, GridItemEventArgs e) 
        { 
            if (e.Item is GridGroupHeaderItem) 
            { 
                var item = e.Item as GridGroupHeaderItem; 
                var groupDataRow = (DataRowView)e.Item.DataItem; 
                var check = new CheckBox(); 
                check.AutoPostBack = true
                check.ID = "CheckAll"
                check.CheckedChanged += check_CheckedChanged; 
                item.DataCell.Controls.Add(check); 
                check.Text = " " + groupDataRow["State"]; 
                item.DataCell.Controls.Add(check); 
            } 
        } 

I think this function should come as standard in RadGrid if possible.
0
Mike
Top achievements
Rank 1
answered on 02 Feb 2009, 01:53 PM

You don't get the following error?
Sys.WebForms.PageRequestmanagerServerErrorException: State is neither a DataColumn nor a DataRelation for table GroupedTable()

This is what the error is pointing to?

 

check.Text =

" " + groupDataRow["State"];

Also, what parts of the prior solution are you using? 

 

0
antoniodlp
Top achievements
Rank 2
answered on 02 Feb 2009, 04:02 PM
Sorry,

The error you're seeing is because of groupDataRow["State"]; which is not in your dataset or data source. Change "State" for the name of the field you want to use in the group headers, like "CategoryID" in the example.

Another diference with the solution is that in my project I pre-set the grouping by using <GroupByExpressions> and don't let the user group dynamically.

Hope it makes sense
Tags
Grid
Asked by
Mike
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Rod
Top achievements
Rank 1
Bodevain Svensson
Top achievements
Rank 1
antoniodlp
Top achievements
Rank 2
Mike
Top achievements
Rank 1
Share this question
or