This is a migrated thread and some comments may be shown as answers.

Retrieving values from GridClientSelectColumn

13 Answers 906 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jack
Top achievements
Rank 1
Jack asked on 26 Jun 2008, 07:00 PM
Hi,

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

Sort by
0
Richard
Top achievements
Rank 1
answered on 26 Jun 2008, 07:59 PM
Hi Jack,

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

Richard
0
Princy
Top achievements
Rank 2
answered on 27 Jun 2008, 07:50 AM
Hi Jack,

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.
0
Acadia
Top achievements
Rank 1
Iron
answered on 27 Aug 2008, 01:15 PM
I tried using the OnSelectedIndexChanged event for my rad grid and the event never fires when I click the checkbox (took it through debugger).  Any idea why?  It seems pretty straight forward. 

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,

0
Sebastian
Telerik team
answered on 27 Aug 2008, 01:24 PM
Hi Acadia,

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.
0
Richard
Top achievements
Rank 1
answered on 27 Aug 2008, 01:26 PM
You should be able to just set in your ClientSettings EnablePostBackOnRowClick="true"
0
Acadia
Top achievements
Rank 1
Iron
answered on 27 Aug 2008, 02:27 PM
I ended up trying it this way and I'm stumped as to why my serverside event easily recognizes the JOB_NUM and JOB_NAME items but cannot detect that the checkbox (Templated Column) is checked.

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

0
Richard
Top achievements
Rank 1
answered on 27 Aug 2008, 02:33 PM
If you change your block:

<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
0
Acadia
Top achievements
Rank 1
Iron
answered on 27 Aug 2008, 02:48 PM
The problem is I have decided I want the user to select a row in the grid  by clicking the checkbox in the row, then clicking the OK button and have that post back.  That's all working fine except for some strange (and frustrating) reason the code behind cannot detect a simple checkbox being checked.  I don't see the difference as far as the vb.net code is concerned between the other two items on the same row, whose values I can easily retrieve, and the state of the checkbox.

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



0
Richard
Top achievements
Rank 1
answered on 27 Aug 2008, 03:15 PM
I don't have time to investigate fully, but my suspicion is that with an unbound column in a grid like that, that the state of the checkboxes is not being recreated on the server.  I'd suggest using the GridClientSelectColumn, set AllowMultiRowSelection="true" in the grid, and in your ClientSettings:

<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
0
Acadia
Top achievements
Rank 1
Iron
answered on 27 Aug 2008, 03:20 PM
Ok I’ll take this and run with it.  You also triggered another idea I can try.  Thanks for all your help Richard!
0
Acadia
Top achievements
Rank 1
Iron
answered on 27 Aug 2008, 08:29 PM
I ended up solving my problem using the following example:
http://www.telerik.com/help/aspnet-ajax/grdselectingrowwithcheckbox_server.html


Thanks to all who helped.
0
Hessner
Top achievements
Rank 2
answered on 30 Aug 2008, 01:57 PM
Glad you found a solution.

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

0
Catherine
Top achievements
Rank 1
answered on 17 Dec 2010, 03:32 AM
I was getting a similar problem - Checked was always False.
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.
Tags
Grid
Asked by
Jack
Top achievements
Rank 1
Answers by
Richard
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Acadia
Top achievements
Rank 1
Iron
Sebastian
Telerik team
Hessner
Top achievements
Rank 2
Catherine
Top achievements
Rank 1
Share this question
or