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

Select all rows on radiobutton click

2 Answers 71 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Naresh
Top achievements
Rank 1
Naresh asked on 23 Sep 2013, 07:36 PM
Hi, I have a radiobutton outside a Radgrid and checkbox inside a radgrid what I want here is when I click on radiobutton all rows inside radgrid should be selected(checkbox should be selected). Please share if you have sample code.

Thanks,
Naresh

2 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 24 Sep 2013, 05:51 AM
Hello,

Server Side

<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource"
           AllowPaging="true">
           <MasterTableView DataKeyNames="ID" CommandItemDisplay="Top">
               <Columns>
                   <telerik:GridEditCommandColumn>
                   </telerik:GridEditCommandColumn>
                   <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID"></telerik:GridBoundColumn>
                   <telerik:GridTemplateColumn>
                       <ItemTemplate>
                           <asp:CheckBox ID="CheckBox1" runat="server" />
                       </ItemTemplate>
                   </telerik:GridTemplateColumn>
               </Columns>
           </MasterTableView>
       </telerik:RadGrid>
 
       <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        dynamic data1 = new[] {
               new { ID = 1, Name ="Name_1",Customdate = DateTime.Now},
               new { ID = 2, Name = "Name_2",Customdate = DateTime.Now},
               new { ID = 3, Name = "Name_3",Customdate = DateTime.Now},
               new { ID = 4, Name = "Name_4",Customdate = DateTime.Now},
               new { ID = 5, Name = "Name_5",Customdate = DateTime.Now}
           };
        RadGrid1.DataSource = data1;
    }
 
 
    protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
        {
            CheckBox CheckBox1 = item.FindControl("CheckBox1") as CheckBox;
            CheckBox1.Checked = true;
        }
    }

Client Side

<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource"
           AllowPaging="true">
           <MasterTableView DataKeyNames="ID" CommandItemDisplay="Top">
               <Columns>
                   <telerik:GridEditCommandColumn>
                   </telerik:GridEditCommandColumn>
                   <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID"></telerik:GridBoundColumn>
                   <telerik:GridTemplateColumn>
                       <ItemTemplate>
                           <asp:CheckBox ID="CheckBox1" runat="server" />
                       </ItemTemplate>
                   </telerik:GridTemplateColumn>
               </Columns>
           </MasterTableView>
       </telerik:RadGrid>
 
       <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return CheckAllCheckBox();" />
function CheckAllCheckBox() {
 
               var grid = $find("<%= RadGrid1.ClientID %>");
               if (grid) {
                   var MasterTable = grid.get_masterTableView();
                   var Rows = MasterTable.get_dataItems();
                   for (var i = 0; i < Rows.length; i++) {
                       var row = Rows[i];
                       var lblName_1 = $(row.get_element()).find("input[id*='CheckBox1']").get(0);
                       lblName_1.checked = true;
                   }
               }
 
               return false;
           }


Thanks,
Jayesh Goyani
0
Princy
Top achievements
Rank 1
answered on 24 Sep 2013, 06:21 AM
Hi Naresh,

Please try the following code snippet to select all rows on RadioButton.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" DataSourceID="SqlDataSource1"
    GridLines="None" AllowPaging="true" AllowMultiRowSelection="true" >
    <MasterTableView DataKeyNames="OrderID">
        <Columns>
            <telerik:GridClientSelectColumn UniqueName="GridClientSelectColumn1">
            </telerik:GridClientSelectColumn>
            <telerik:GridBoundColumn UniqueName="OrderID" DataField="OrderID" HeaderText="OrderID" />
            <telerik:GridBoundColumn DataField="ShipCity" HeaderText="ShipCity" UniqueName="ShipCity" />         
        </Columns>
    </MasterTableView>
    <ClientSettings Selecting-AllowRowSelect="true">
    </ClientSettings>
</telerik:RadGrid>
<asp:RadioButton ID="RadioButton1" runat="server" OnCheckedChanged="RadioButton1_CheckedChanged"
    AutoPostBack="true" />

C#:
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
   {      
       foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
       {               
           item.Selected = true;           
       }
   }

Thanks,
Princy
Tags
Grid
Asked by
Naresh
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Princy
Top achievements
Rank 1
Share this question
or