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

GridClientSelectColumn select all

5 Answers 711 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Dario Zanelli
Top achievements
Rank 1
Dario Zanelli asked on 27 Dec 2010, 05:43 PM
Hi all,

is there a way of manage (via code behind) the checkbox in the header that comes with a GridClientSelectColumn?
I couldn't find the right way. I also tried to manage it via an event but it doesn't fire.
Here's a snippet:

...code
GridHeaderItem headerItem = (GridHeaderItem)RadGridDocumentsControlForm.MasterTableView.GetItems(GridItemType.Header)[0];
CheckBox chkSelectAll = headerItem.FindControl("CheckedSelectCheckBox") as CheckBox;
chkSelectAll.Enabled = true;
chkSelectAll.CheckedChanged +=new EventHandler(chkSelectAll_CheckedChanged);
...code

//handler
void chkSelectAll_CheckedChanged(object sender, EventArgs e)
{
            //code
 }

Thanks in advance.

Dario Zanelli

5 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 27 Dec 2010, 10:50 PM
Hello Dario,

Basically the cleanest solution would be to create your own select column based on GridTemplateColumn. This way you will have a full control over this functionality.
Nevertheless, if you provide some more information about the desired behavior of the header checkbox I will be able to give more to-the-point solution for your scenario.

Greetings,
Daniel
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
Dario Zanelli
Top achievements
Rank 1
answered on 28 Dec 2010, 02:04 AM
Hi Daniel,

thanks for your answer. I'd like to perform some operations when the checkAll checkbox is checked or not (actually it is not important which code behing I'd like to run, just I need to run something).

I was expecting it was easier to manage that control without changing the whole code I've already developed. Let me know if there is something I can do.

Regards,

Dario Zanelli
0
Princy
Top achievements
Rank 2
answered on 28 Dec 2010, 10:39 AM
Hello Dario,

The following code snippet shows how to attach 'CheckedChanged' event to header CheckBox in GridClientSelectColumn.

ASPX:
<telerik:GridClientSelectColumn UniqueName="GridClientSelectColumn">
</telerik:GridClientSelectColumn>

C#:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
    {
       if (e.Item is GridHeaderItem)
        {
            GridHeaderItem headerItem = (GridHeaderItem)e.Item;
            CheckBox chkSelectAll = (CheckBox)headerItem["GridClientSelectColumn"].Controls[0];
            chkSelectAll.AutoPostBack = true;
            chkSelectAll.CheckedChanged += new EventHandler(chkSelectAll_CheckedChanged);
        }
    }
 
    void chkSelectAll_CheckedChanged(object sender, EventArgs e)
    {
        
    }

Thanks,
Princy.
0
Dario Zanelli
Top achievements
Rank 1
answered on 03 Jan 2011, 05:32 PM
Hi Princy,
the code you sent me is working, but only when I am selecting all the items.
When I am deselecting them, the code inside the function chkSelectAll_CheckedChanged is not executed.

Can you help me in solving this issue?

Thanks a lot,

Dario
0
Princy
Top achievements
Rank 2
answered on 04 Jan 2011, 08:57 AM
Hello Dario,

The above code is working fine at my end. Then one suggestion is to try the following approach to achieve your requirement.

ASPX:
<telerik:RadGrid ID="RadGrid2" runat="server" DataSourceID="SqlDataSource1"
    AllowMultiRowSelection="true">
    <MasterTableView>
          <Columns>
            <telerik:GridTemplateColumn UniqueName="TemplateColumn">
                <HeaderTemplate>
                    <asp:CheckBox ID="HeaderCheckBox" runat="server" AutoPostBack="true" OnCheckedChanged="HeaderCheckBox_CheckedChanged">
                    </asp:CheckBox>
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox1" onclick="CheckItem(this);" runat="server"></asp:CheckBox>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
        </Columns>
    </MasterTableView>
    <ClientSettings Selecting-AllowRowSelect="true">
         <ClientEvents  OnGridCreated="gridCreated" />
    </ClientSettings>
</telerik:RadGrid>

Java Script:
<script type="text/javascript">
   
    var RadGrid1;
    var DataItems;
    function gridCreated(sender, args) {
        RadGrid1 = sender;
    }
    function CheckItem(itemCheckBox)
      {
        var masterTableView = RadGrid1.get_masterTableView();
        if (DataItems == null)
           {
            DataItems = masterTableView.get_dataItems();
        }
        var row = itemCheckBox.parentNode.parentNode;
        if (row.tagName === "TR" && row.id != "")
            {
            var item = $find(row.id);
            if (!item.get_selected() && itemCheckBox.checked)
                 {
                masterTableView.clearSelectedItems();
                item.set_selected(true);
            }
        }
     }
     
</script>

C#:
protected void HeaderCheckBox_CheckedChanged(object sender, EventArgs e)
   {
       CheckBox headercheckbox = (CheckBox)sender;
       if (headercheckbox.Checked)
       {
           foreach (GridDataItem item in RadGrid2.Items)
           {
               item.Selected = headercheckbox.Checked;
               CheckBox checkbox = (CheckBox)item.FindControl("CheckBox1");
               checkbox.Checked = headercheckbox.Checked;
           }
       }
   }

Thanks,
Princy.
Tags
Grid
Asked by
Dario Zanelli
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Dario Zanelli
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Share this question
or