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

Header Checkbox sets all checkboxes

5 Answers 191 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Bill
Top achievements
Rank 1
Bill asked on 01 Feb 2011, 12:44 AM
I have a grid that when put in edit mode has a popup editform that has another grid containing records with checkboxes in the grid column edit template. I want to add a checkbox in the header that when selected will toggle the checkboxes in all the rows to checked or unchecked. Here is the column definition in my editforms child grid:
<telerik:GridTemplateColumn UniqueName="Resubmitted" DataField="Resubmitted" 
                                        HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" HeaderStyle-Width="20%" >
                                        <HeaderTemplate>
                                         <asp:CheckBox id="headerChkbox" OnCheckedChanged="ToggleSelectedState" AutoPostBack="true" runat="server" Text=" resubmitted" ></asp:CheckBox>
                                        </HeaderTemplate>
                                        <EditItemTemplate>
                                           <asp:CheckBox id="chkResubmittedEdit" runat="server" Checked='<%# Bind("Resubmitted") %>'/>
                                        </EditItemTemplate>
                                        <ItemTemplate>
                                            <asp:CheckBox id="chkResubmittedItem" runat="server" Checked='<%# Eval("Resubmitted") %>' />
                                        </ItemTemplate>
                                    </telerik:GridTemplateColumn>

I am getting a null value when trying to access the items in the ToggleSelectedState method for the grid items. Here is my code-behind in c#:
protected void ToggleSelectedState(object sender, EventArgs e)
        {
            CheckBox headerCheckBox = (sender as CheckBox);
            try
            {
               GridDataItemCollection items = (grdClaimHist.FindControl("grdClaimDetail") as RadGrid).MasterTableView.Items;
                foreach (GridDataItem dataItem in items)
                {
                    CheckBox chk = (dataItem.FindControl("chkResubmittedEdit") as CheckBox);
                    chk.Checked = headerCheckBox.Checked;
                }
            }
            catch (Exception ex)
            {
                //continue
            }
        }
grdClaimHist is the parent grid that contains the editform  while grdClaimDetail is the child grid. Is there another way I should be trying to implement this functionality since the ToggleSelectedState method is called on my header checkbox click?


5 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 01 Feb 2011, 08:35 AM
Hello Bill,

Give a try with the following code snippet and check whether it works now.

C#:
protected void ToggleSelectedState(object sender, EventArgs e)
    {
        CheckBox headerCheckBox = (sender as CheckBox);
        RadGrid grdClaimDetail = (RadGrid)headerCheckBox.NamingContainer.NamingContainer.NamingContainer.NamingContainer;
        foreach (GridDataItem dataItem in grdClaimDetail.Items)
            {
                CheckBox chk = (dataItem.FindControl("chkResubmittedItem") as CheckBox);
                chk.Checked = headerCheckBox.Checked;
            }
    }

Thanks,
Princy.
0
Bill
Top achievements
Rank 1
answered on 03 Feb 2011, 04:57 PM
Thank you, Part of that worked. This is what I need to get to the values:
Hashtable values = new Hashtable();
RadGrid grdClaimDetail = (RadGrid)  headerCheckBox.NamingContainer.NamingContainer.NamingContainer.NamingContainer;
for (int i = 0; i < grdClaimDetail.EditItems.Count; i++)
{
    GridItem item = grdClaimDetail.EditItems[i];
    ReprocessClaimHistory claim = (item.DataItem as ReprocessClaimHistory);
    //The GridTableView will fill the values from all editable columns in the hash
    item.OwnerTableView.ExtractValuesFromItem(values, (grdClaimDetail.EditItems[i] as GridEditableItem));
    // get update values
    IDictionaryEnumerator en = values.GetEnumerator();
    while (en.MoveNext())
    {
        string key = en.Key.ToString();
        switch (key)
        {
            case "RunNumber":
                RunNumber = Convert.ToInt32(en.Value.ToString());
                break;
            case "Status":
                ClaimStatus = Convert.ToInt32(en.Value.ToString());
                break;
            case "ClaimNumber":
                ClaimNumber = en.Value.ToString();
                break;
            case "Resubmitted":
                Resubmitted = Convert.ToBoolean(en.Value.ToString());
                // change the resubmitted value to the header checked value
                if (Resubmitted != headerCheckBox.Checked)
                {
                    Resubmitted = headerCheckBox.Checked;
                      
                }
                break;
            default:
                break;
        }
    }

I want to actually edit the "Resubmitted" value and then when my 'save' button is clicked I can iterate through the edititems and save my changes.
I tried using en.Value in an assignment statement but it only has a 'getter' and doesn't support setting the value. Is there Another way to iterate through the edit items and set the values for a specific key?
0
Princy
Top achievements
Rank 2
answered on 04 Feb 2011, 08:06 AM
Hello Bill,

Try the following way to toops through each edit item in grid.

C#:
protected void ToggleSelectedState(object sender, EventArgs e)
    {
        CheckBox headerCheckBox = (sender as CheckBox);
        RadGrid grdClaimDetail = (RadGrid)headerCheckBox.NamingContainer.NamingContainer.NamingContainer.NamingContainer;
        foreach (GridEditableItem editItem in grdClaimDetail.MasterTableView.GetItems(GridItemType.EditItem))
        {
            CheckBox chk = (editItem.FindControl("chkResubmittedEdit") as CheckBox);
            chk.Checked = headerCheckBox.Checked;
        }
    }

Thanks,
Princy.
0
Bill
Top achievements
Rank 1
answered on 08 Feb 2011, 09:14 PM
Princy,
That worked with some modification to let me access the editable items and iterate through them to check them all. I have an unexpected side effect now though. Here is my Toggle code:
CheckBox headerCheckBox = (sender as CheckBox);
                        ...
            try
            {
                Hashtable values = new Hashtable();
                RadGrid grdClaimDetail = (RadGrid)headerCheckBox.NamingContainer.NamingContainer.NamingContainer.NamingContainer;
                List<ReprocessClaimHistory> claimlist = new List<ReprocessClaimHistory>();
  
                for (int i = 0; i < grdClaimDetail.EditItems.Count; i++)
                {
                    GridItem item = grdClaimDetail.EditItems[i];
                    ReprocessClaimHistory claim = (item.DataItem as ReprocessClaimHistory);
  
                    //The GridTableView will fill the values from all editable columns in the hash
                    item.OwnerTableView.ExtractValuesFromItem(values, (grdClaimDetail.EditItems[i] as GridEditableItem));
                    //values["Resubmitted"] = headerCheckBox.Checked;
                    ReprocessClaimHistory newItem = new ReprocessClaimHistory(); 
                           ...
                    newItem.Resubmitted = headerCheckBox.Checked;
                    claimlist.Add(newItem);
                }
                grdClaimDetail.DataSource = claimlist;
                grdClaimDetail.DataBind();
            }
            catch (Exception ex)
            {
                //continue
            }

It does 'check' all my checkboxes but it does not span across multiple pages in the grid if I have them. For instance, my page size is 10 and I have 20 records the grid will display my 10 records and that is all. What I thought would happen is that it would update all the checkboxes keeping the state of the edit items which all 20 should be updated and on my save button click save all the current checked items. The user would use the select all and then uncheck some of the checkboxes then click save. How can I accomplish this functionality?
0
Princy
Top achievements
Rank 2
answered on 09 Feb 2011, 06:33 AM
Hello Bill,

When paging is enabled in the grid, a solution would be to temporarily disable paging, loop through all the edit items in grid, select the items and then enable paging back.
Also refer the following forum post to persist the selected state of CheckBox on paging.
Check-box template column loses status after paging

Hope this helps,
Princy.
Tags
Grid
Asked by
Bill
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Bill
Top achievements
Rank 1
Share this question
or