I have a GridClientSelectColumn in a RadGrid. When a client selects a row, I would like to retrieve a value from the selected row and save it to a variable. I have tried using the ItemCreated, ItemCommand, and ItemEvent. So far I've had no luck... any suggestions would be appreciated.
Thanks,
Jack
13 Answers, 1 is accepted

You can handle this client side using the ClientSettings-ClientEvents-OnRowSelected event, or server side using the OnSelectedIndexChanged event
Richard

You can also try the following code snippet in the SelectedIndexChanged event.
CS:
protected void RadGrid1_SelectedIndexChanged(object sender, EventArgs e) |
{ |
foreach (GridDataItem item in RadGrid1.MasterTableView.Items) |
{ |
if (item.Selected) |
{ |
string strtxt = item["columnUniqueName"].Text.ToString(); |
} |
} |
} |
Thanks
Princy.

Here is my grid with the GridClientSelectColumn:
<telerik:RadGrid ID="rgJ" runat="server" Skin="WebBlue" AutoGenerateColumns="False"
GridLines="None" AllowPaging="True" PageSize="15" AllowFilteringByColumn="True"
AllowSorting="True" Width="98%" AllowMultiRowSelection="false">
<MasterTableView CommandItemDisplay="None">
<Columns>
<telerik:GridClientSelectColumn HeaderText="Select" UniqueName="SELECT_ROW" >
<HeaderStyle HorizontalAlign="Left" Width="40px"/>
<ItemStyle HorizontalAlign="Center" Width="40px" />
</telerik:GridClientSelectColumn>
<telerik:GridBoundColumn DataField="JOB_CODE" HeaderText="Job Code" UniqueName="JOB_CODE"
HeaderStyle-Wrap="false" ItemStyle-Wrap="false" ReadOnly="true">
<HeaderStyle HorizontalAlign="Left" Width="100px" />
<ItemStyle HorizontalAlign="Right" Width="100px" />
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="JOB_NAME" HeaderText="Job Name" UniqueName="JOB_NAME"
HeaderStyle-Wrap="false" ItemStyle-Wrap="True" ReadOnly="true">
<HeaderStyle HorizontalAlign="Left" Width="150px" />
<ItemStyle HorizontalAlign="Center" Width="150px" Wrap="true" />
</telerik:GridBoundColumn>
</Columns>
<ExpandCollapseColumn Resizable="False" Visible="False">
<HeaderStyle Width="20px" />
</ExpandCollapseColumn>
</MasterTableView>
<ClientSettings>
<Scrolling AllowScroll="True" UseStaticHeaders="True" />
<Selecting AllowRowSelect="True" />
</ClientSettings>
</telerik:RadGrid>
And my vb.net code behind handler:
Private
Sub rgJ_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rgJ.SelectedIndexChanged
Dim JobNumName As String = ""
For Each item As GridDataItem In rgJ.MasterTableView.Items
If (item.Selected) Then
JobNumName = item(
"JOB_NAME").Text.ToString & "-->" & item("JOB_CODE").Text.ToString
End If
Next
End Sub
There really isn't much to it I just can't get the serverside sub to fire when I click the checkbox of a row i n the gird. Any help would be greatly appreciated.
Thanks,
Through the checkboxes inside a GridClientSelectColumn you perform pure client-side selection. Therefore it is expected that the SelectedIndexChanged server event is not raised since there is no postback to the server.
If you would like to have your checkbox selection "connected" to some server-side logic, consider the solution presented in this online demo of RadGrid:
http://demos.telerik.com/ASPNET/Prometheus/Grid/Examples/Programming/SelectRowWithCheckBox/DefaultCS.aspx
Best regards,
Stephen
the Telerik team
Check out Telerik Trainer, the state of the art learning tool for Telerik products.


Here is my simple grid:
<
telerik:RadGrid ID="rgJ" runat="server" Skin="WebBlue" AutoGenerateColumns="False"
GridLines="None" AllowPaging="True" PageSize="15" AllowFilteringByColumn="True"
AllowSorting="True" Width="98%" AllowMultiRowSelection="false">
<MasterTableView CommandItemDisplay="None">
<Columns>
<telerik:GridTemplateColumn HeaderText="Select Job" UniqueName="SELECT_ROW" AllowFiltering="False">
<HeaderStyle HorizontalAlign="Left" Width="60px" Wrap="False" />
<ItemStyle HorizontalAlign="Left" Width="60px" Wrap="False" />
<ItemTemplate>
<asp:CheckBox ID="cbJS" runat="server" />
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridBoundColumn DataField="JOB_CODE" HeaderText="Job Code" UniqueName="JOB_CODE"
HeaderStyle-Wrap="false" ItemStyle-Wrap="false" ReadOnly="true">
<HeaderStyle HorizontalAlign="Left" Width="100px" />
<ItemStyle HorizontalAlign="Right" Width="100px" />
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="JOB_NAME" HeaderText="Job Name" UniqueName="JOB_NAME"
HeaderStyle-Wrap="false" ItemStyle-Wrap="True" ReadOnly="true">
<HeaderStyle HorizontalAlign="Left" Width="150px" />
<ItemStyle HorizontalAlign="Center" Width="150px" Wrap="true" />
</telerik:GridBoundColumn>
</Columns>
<ExpandCollapseColumn Resizable="False" Visible="False">
<HeaderStyle Width="20px" />
</ExpandCollapseColumn>
</MasterTableView>
<ClientSettings>
<Scrolling AllowScroll="True" UseStaticHeaders="True" />
<Selecting />
</ClientSettings>
</telerik:RadGrid>
My user selects a row by clicking the checkbox for that row, then they clikc the OK button which posts back to the following server code.
Protected
Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
Dim JobNumName As String = ""
For Each item As GridDataItem In rgJ.Items
Dim cbJs As CheckBox = CType(item.FindControl("cbJS"), CheckBox)
If (cbJs.Checked) Then 'THE CODE NEVER DETECTS THAT THIS CHECKBOX IS CHECKED. IT ALWAYS SAYS FALSE! WHY???
JobNumName = item(
"JOB_NAME").Text.ToString & "-->" & item("JOB_CODE").Text.ToString
End If
Next
End Sub
Could somebody please explain to me why the code does not recognize a checked checkbox in a templated column in the grid, but instead always sees it as false.
Thanks

<ClientSettings>
<Scrolling AllowScroll="True" UseStaticHeaders="True" />
<Selecting />
</ClientSettings>
to:<ClientSettings EnablePostbackOnRowClick="True" >
<Scrolling AllowScroll="True" UseStaticHeaders="True" />
<Selecting AllowRowSelect="True" />
</ClientSettings>
You should get your postback and the SelectedIndexChanged event will fire
I mean what could be wrong with this sub? I get an instance of the checkbox from each row and I check its state (checked). It says FALSE every time for the row that is actually checked.
Protected Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
Dim JobNumName As String = ""
For Each item As GridDataItem In rgJ.Items
Dim cbJs As CheckBox = CType(item.FindControl("cbJS"), CheckBox)
If (cbJs.Checked) Then 'False, false, false it says
JobNumName = item(
"JOB_NAME").Text.ToString & "-->" & item("JOB_CODE").Text.ToString
End If
Next
End Sub

<ClientSettings EnablePostBackOnRowClick="false">
<Selecting AllowRowSelect="true" />
</ClientSettings>
Then in your button click handler you can use the rgJ.SelectedItems collection to get the items that have been checked.
Richard


http://www.telerik.com/help/aspnet-ajax/grdselectingrowwithcheckbox_server.html
Thanks to all who helped.

I use this in codebehind:
foreach (GridItem Item in TheGrid.SelectedItems) |
{ |
string someId = TheGrid.Items[Item.ItemIndex].Cells[3].Text; |
} |
Here I get my id's when using: GridClientSelectColumn.
"I have another challenge,- I want to keep the status of GridClientSelect Column on return)." - no problems here,- just missed a "wrong" databind().
Regards,
Hessner

Turns out that someone's code on Page_Load was rebinding the grid on PostBack and thus clearing the checkbox values.
Removed the unneccessary call and all was fine. Argh!
Oh. Also have AutoPostBack="False" for the Checkbox.