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

18 posts, 0 answers
  1. Swapnil
    Swapnil avatar
    96 posts
    Member since:
    Aug 2013

    Posted 11 Aug 2013 Link to this post

    Hi,
    how to add checkbox column in radgrid and get ids associated with that record on button click event?
    Thanks
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 12 Aug 2013 Link to this post

    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
  3. Swapnil
    Swapnil avatar
    96 posts
    Member since:
    Aug 2013

    Posted 12 Aug 2013 Link to this post

    I am not able to check the checkbox ,looks like just read only
    Thanks
  4. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 12 Aug 2013 Link to this post

    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
  5. Swapnil
    Swapnil avatar
    96 posts
    Member since:
    Aug 2013

    Posted 12 Aug 2013 Link to this post

    used debugger and your complete  code but each time its coming out of if condition saying checkbox checked="false"
    Thanks
  6. Jayesh Goyani
    Jayesh Goyani avatar
    2733 posts
    Member since:
    May 2010

    Posted 12 Aug 2013 Link to this post

    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
  7. Jayesh Goyani
    Jayesh Goyani avatar
    2733 posts
    Member since:
    May 2010

    Posted 12 Aug 2013 Link to this post

    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
  8. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 13 Aug 2013 Link to this post

    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
  9. Brad
    Brad avatar
    6 posts
    Member since:
    Mar 2014

    Posted 07 Apr 2014 Link to this post

    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.
  10. Suresh
    Suresh avatar
    4 posts
    Member since:
    Aug 2014

    Posted 07 Aug 2014 in reply to Princy Link to this post

    How can I do this in winforms...
    how to get checked checkbox rows in radgridview...
  11. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    2066 posts

    Posted 11 Aug 2014 Link to this post

    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.

     
  12. Alvis
    Alvis avatar
    1 posts
    Member since:
    Aug 2016

    Posted 27 Sep 2016 in reply to Jayesh Goyani Link to this post

    I done same , but Check box not showing.. 
  13. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    2066 posts

    Posted 28 Sep 2016 Link to this post

    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.
  14. Kristopher
    Kristopher avatar
    5 posts
    Member since:
    Jan 2014

    Posted 09 Jan in reply to Jayesh Goyani Link to this post

    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??
  15. Jayesh Goyani
    Jayesh Goyani avatar
    2733 posts
    Member since:
    May 2010

    Posted 10 Jan in reply to Kristopher Link to this post

    Hello Krishtopher,

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

    Thanks, Jayesh Goyani

  16. Kristopher
    Kristopher avatar
    5 posts
    Member since:
    Jan 2014

    Posted 10 Jan in reply to Jayesh Goyani Link to this post

    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);
                   }
  17. Kristopher
    Kristopher avatar
    5 posts
    Member since:
    Jan 2014

    Posted 10 Jan in reply to Kristopher Link to this post

    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;
  18. Sweta
    Sweta avatar
    1 posts
    Member since:
    Jun 2017

    Posted 20 Jun in reply to Jayesh Goyani Link to this post

    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
Back to Top