Add check box clumn in radgrid and get id of checked records on button click(Save)

2 Answers 6534 Views
Grid
Swapnil
Top achievements
Rank 1
Swapnil asked on 12 Aug 2013, 04:53 AM
Hi,
how to add checkbox column in radgrid and get ids associated with that record on button click event?
Thanks

2 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 12 Aug 2013, 05:51 AM
Hi Swapnil,

I guess you want to add a GridCheckBoxColumn.Please try the below code snippet,which shows how to add GridCheckBoxColumn ,and access the checked rows using the datakey value.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server"
    <MasterTableView DataKeyNames="OrderID">
        <Columns>         
            <telerik:GridCheckBoxColumn UniqueName="GridCheckBoxColumn" DataField="IsTrue">
            </telerik:GridCheckBoxColumn>       
        </Columns>
    </MasterTableView>
</telerik:RadGrid>
<asp:Button ID="Button1" runat="server" Text="Save" OnClick="Button1_Click" />

C#:
protected void Button1_Click(object sender, EventArgs e)
   {
       foreach (GridItem item in RadGrid1.MasterTableView.Items)
       {
           GridDataItem dataitem = (GridDataItem)item;
           TableCell cell = dataitem["GridCheckBoxColumn"];
           CheckBox checkBox = (CheckBox)cell.Controls[0];
           if (checkBox.Checked)
           {
            string value = dataitem.GetDataKeyValue("OrderID").ToString(); //Access the checked row using DataKeyNames
           }
       }
 }

Thanks,
Princy
Swapnil
Top achievements
Rank 1
commented on 12 Aug 2013, 06:03 AM

I am not able to check the checkbox ,looks like just read only
Thanks
Princy
Top achievements
Rank 2
commented on 12 Aug 2013, 06:41 AM

Hi Swapnil,

If you use GridCheckBoxColumn,it will be disabled in view mode.It is enabled only in insert or edit mode.If you want to check in view mode,you can use a GridClientSelectColumn,or you can add a checkbox in GridTemplateColumn.
To access the GridClientSelectColumn,please try the following code snippet.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" AllowMultiRowSelection="true">
    <ClientSettings Selecting-AllowRowSelect="true">
    </ClientSettings>
    <MasterTableView DataKeyNames="OrderID">
        <Columns>        
            <telerik:GridClientSelectColumn UniqueName="ClientSelectColumn" />        
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

C#:
protected void Button1_Click(object sender, EventArgs e)
   {
       foreach (GridItem item in RadGrid1.MasterTableView.Items)
       {
           GridDataItem dataitem = (GridDataItem)item;
           TableCell cell = dataitem["ClientSelectColumn"];
           CheckBox checkBox = (CheckBox)cell.Controls[0];
           if (checkBox.Checked)
           {
             string value = dataitem.GetDataKeyValue("OrderID").ToString();              
           }
       }
  }


Thanks,
Princy
Jayesh Goyani
Top achievements
Rank 2
commented on 12 Aug 2013, 10:50 AM

Hello,

If you want to use @princy's code then try with the below code snippet.

protected void Button1_Click(object sender, EventArgs e)
   {
       foreach (GridItem item in RadGrid1.MasterTableView.Items)
       {
           GridDataItem dataitem = (GridDataItem)item;
           if (dataitem.Selected)
           {
               string value = dataitem.GetDataKeyValue("OrderID").ToString();
           }
       }
   }

Note : please use Item.selected inplace of checkbox.checked.

Thanks,
Jayesh Goyani
Princy
Top achievements
Rank 2
commented on 13 Aug 2013, 03:01 PM

Hi Swapnil,

Make sure that you have no postback,if so please set it inside the condition if(!IsPostBack), because in case if there is postback always the default value of the checkbox will be taken,which is false.
Please let me know if any concern.

Thanks,
Princy
Brad
Top achievements
Rank 1
commented on 07 Apr 2014, 09:01 PM

The second example worked fine for me, but since I see comments that there was an issue and a follow-up, I will test some more to make sure.  But everything looks find for me.
Suresh
Top achievements
Rank 1
commented on 07 Aug 2014, 05:50 AM

How can I do this in winforms...
how to get checked checkbox rows in radgridview...
Konstantin Dikov
Telerik team
commented on 11 Aug 2014, 02:30 PM

Hello Suresh,

Could you please open a forum thread in the category/product that you are using. 

For your convenience, following is a link to the GridView threads for UI for WinForms:
Hope this helps.


Regards,
Konstantin Dikov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Alvis
Top achievements
Rank 1
commented on 28 Sep 2016, 03:27 AM

I done same , but Check box not showing.. 
Konstantin Dikov
Telerik team
commented on 28 Sep 2016, 08:35 AM

Hello Alvis,

Could you please provide more information on your scenario and what you have tried for displaying the checkboxes? 

Looking forward to your reply.

Regards,
Konstantin Dikov
Telerik by Progress
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
Supriya
Top achievements
Rank 1
commented on 26 Oct 2017, 05:36 AM

hi 

as attached document i want to find values of row and column of selected checkbox on button click.

 

can you please help me?

Pawan
Top achievements
Rank 1
commented on 22 Jul 2020, 05:19 AM

Hello,

I have a grid with Autogenerated gridcheckboxcolumn in that I want pop up warning yes and cancel cancel how to achieve that

Pawan
Top achievements
Rank 1
commented on 22 Jul 2020, 05:30 AM

Example I have a grid on button click then in that grid I have column binding with checkbix item templaye in that every column is bounded with checkbox  with by default check now I want to put pop up  warning yes and cancel when checkbox is unchecked.

How can I achieve this ? Where we can write it in the radgrid ? How to get the value of checkbox weather it is checked or not as it is autogenerated with default check suppose if we unselect the product of subscription then it shows really you want remove that product from subscriptionnlike that. Request to help me on this topis please.

Eyup
Telerik team
commented on 25 Jul 2020, 06:07 AM

Hi Pawan,

 

This requirement is possible, you can check the attached web site samples which demonstrate a similar implementation.

I hope this will prove helpful.

 

Regards,
Eyup
Progress Telerik

0
Swapnil
Top achievements
Rank 1
answered on 12 Aug 2013, 07:01 AM
used debugger and your complete  code but each time its coming out of if condition saying checkbox checked="false"
Thanks
Jayesh Goyani
Top achievements
Rank 2
commented on 12 Aug 2013, 10:38 AM

Hello,

Please try with the below code snippet.

<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" OnNeedDataSource="RadGrid1_NeedDataSource">
    <MasterTableView DataKeyNames="ID">
        <Columns>
            <telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name">
            </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"},
             new { ID = 2, Name = "Name_2"},
             new { ID = 3, Name = "Name_1"},
             new { ID = 4, Name = "Name_4"},
             new { ID = 5, Name = "Name_1"}
         };
 
       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;
           if (CheckBox1 != null && CheckBox1.Checked)
           {
               string strKey = item.GetDataKeyValue("ID").ToString();
           }
       }
   }


Thanks,
Jayesh Goyani
Kristopher
Top achievements
Rank 1
commented on 10 Jan 2017, 02:27 AM

Your example of obtaining datakeys of selected items only returns a single value.  I would like to see an example of storing the selected values in comma separated string??
Jayesh Goyani
Top achievements
Rank 2
commented on 10 Jan 2017, 11:50 AM

Hello Krishtopher,

Could you please elaborate your scenario and share your code what have you tried?

Thanks, Jayesh Goyani

Kristopher
Top achievements
Rank 1
commented on 10 Jan 2017, 10:24 PM

Thank you for responding.  I have a radgrid with a checkbox column (template column) of records that i want to select and save each selection that contains a unique recordID ("CN") into a comma separated string that can be stored into a session variable that i can retrieve elsewhere.   I adapted your suggested snippet that you posted on 12 Aug 2013 but it only saves one value?  I need all the selected values in my column "CN" saved, either separated by a comma or space or however.   This is what i got so far but it is not working.

foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
           {
               //GridDataItem dataitem = (GridDataItem)item;
               CheckBox chk = (CheckBox)item.FindControl("CheckBox1");
               if (chk.Checked)
               {
                   string value = item.GetDataKeyValue("CN").ToString();
                   string items = string.Empty;
                   foreach (ListItem i in chk.Items)
                   {
                       if (i.Selected == true)
                       {
                           items += i.Text + ",";
                       }
                   }
                   Response.Write("selected items" + items);
               }
Kristopher
Top achievements
Rank 1
commented on 11 Jan 2017, 12:18 AM

I got this to work, in case anyone wants to know

string items = string.Empty;
foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
{
    CheckBox chk = (CheckBox)item.FindControl("CheckBox1");
    string value = item.GetDataKeyValue("CN").ToString();
     
    if (chk.Checked)
    {
         
        items += value + ",";
 
    }
 
     
}
var list = items.TrimEnd(',');
Session["mySelection"] = list;
Sweta
Top achievements
Rank 1
commented on 20 Jun 2017, 01:46 PM

Hello
I have use the code as you wrote above. I am facing problem at the time of  inserting a new record, checkbox column is not generating while it is visible when i am coming from grid
Tags
Grid
Asked by
Swapnil
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Swapnil
Top achievements
Rank 1
Share this question
or