2 Answers, 1 is accepted
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
Thanks
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
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
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
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.
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
hi
as attached document i want to find values of row and column of selected checkbox on button click.
can you please help me?
Hello,
I have a grid with Autogenerated gridcheckboxcolumn in that I want pop up warning yes and cancel cancel how to achieve that
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.
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
Thanks
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
Hello Krishtopher,
Could you please elaborate your scenario and share your code what have you tried?
Thanks, Jayesh Goyani
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); }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;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