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

13 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. UI for ASP.NET Ajax is Ready for VS 2017
  4. 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
  5. 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
  6. 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
  7. Jayesh Goyani
    Jayesh Goyani avatar
    2732 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
  8. Jayesh Goyani
    Jayesh Goyani avatar
    2732 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
  9. 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
  10. 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.
  11. 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...
  12. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    1800 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.

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

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

    I done same , but Check box not showing.. 
  14. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    1800 posts

    Posted 28 Sep 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.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017