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

Rad Grid with Checkbox Column

3 Answers 1908 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kelvin
Top achievements
Rank 1
Kelvin asked on 24 Sep 2012, 04:00 AM
Hi,

Please excuse my confusion on this!  What I am attempting to do is to place a checkbox column in a grid, allow the user to select one or more checkboxes, then have a button on the page that kicks off an Ajax call to the code behind.  The code behind would loop through the grid and if the checkbox for that row is checked read a column that contains the unique key value and do something with it.  I have tried with the various column types to no avail, maybe I have a setting wrong...not sure.  Anyway can someone provide some assistance?

Here is what my simple test column looks like...the second column is the ID column
<telerik:RadGrid ID="RadGridAdHoc" runat="server" Width="100%" Height="160px" CssClass="center" AllowPaging="true" AllowSorting="true" AllowCustomPaging="False" PageSize="30" EnableAJAX="False" EnableAJAXLoadingTemplate="true" EnableEmbeddedSkins='true' Skin="Office2007" EnableViewState='false'>
<PagerStyle AlwaysVisible='true' Mode='NextPrevAndNumeric' />
<ClientSettings>
<ClientEvents />
 <Selecting AllowRowSelect="true" EnableDragToSelectRows="false" UseClientSelectColumnOnly='true' />
<Scrolling UseStaticHeaders="True" AllowScroll="true" SaveScrollPosition="false" />
 <Resizing AllowColumnResize="False" />
 </ClientSettings>
 <MasterTableView AllowAutomaticUpdates="true" AutoGenerateColumns="false" AllowMultiColumnSorting="False" TableLayout="fixed" >
 <Columns>
<telerik:GridTemplateColumn HeaderText="Select"  UniqueName="ConfirmOrder" DataField='DataIDValue'>
<ItemTemplate>
 <asp:CheckBox ID="CheckBox1" runat="server" />
 </ItemTemplate>
</telerik:GridTemplateColumn>      
 <telerik:GridBoundColumn DataField="DataIDValue" UniqueName="DataIDValue" Display='false'>
 </telerik:GridBoundColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>

Here is what is in my code behind looks like..The ajax call on the button click works fine and it gets the the sub holding the code below however it does not ever enter the loop....the RadGridAdHoc.MasterTableView.Items count = 0 so skips over it.

     For Each item As GridDataItem In RadGridAdHoc.MasterTableView.Items
            For Each col As GridColumn In RadGridAdHoc.MasterTableView.RenderColumns
                If col.UniqueName = "Confirm" Then
                    Dim chkbx As CheckBox = DirectCast(item("ConfirmOrder").Controls(0), CheckBox)
                    If chkbx.Checked = True Then
                        'Get ID value from row with checked checkbox
                        'How do I get the value if the DataID column here?
                    End If
                End If
            Next
        Next

I guess also not really sure what column type is best to use...I have tried the GridCheckBoxColumn and the GridClientSelectColumn without success....and now GridTemplateColumn.....the maybe one type is better to use?

How can I retrieve the value of the DataID column once the checkbox is identified as checked?  

Any assistance anyone could provide would be greatly appreciated!  Thanks in advance,

Kelvin

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 24 Sep 2012, 04:19 AM
Hi Kevin,

Try looping through the items as shown below.
VB:
For Each item As GridDataItem In RadGrid1.Items
    Dim chk As CheckBox = DirectCast(item.FindControl("CheckBox1"), CheckBox)
    If chk.Checked = True Then
        Dim value As String = item("DataIDValue").Text
    End If
Next

Thanks,
Shinu.
0
Kelvin
Top achievements
Rank 1
answered on 24 Sep 2012, 02:37 PM
Shinu...Thank you very much for the quick reply....it didn't work at first until I changed the grid setting EnableViewState to true then it works great!

I'll guess I will reload the page after the ajax request or else I am going to end up with a corrupted view state...unless anyone can provide a solution.  I would rather just reload the grid rather than the entire page as there are a lot of other data points on the page...any thoughts?

Also, sort of a learning thing...with a checkbox column is any one type preferred over the other for this type of behavior?  Just curious really.

Thanks in advance as always!

Kelvin
0
Accepted
Shinu
Top achievements
Rank 2
answered on 25 Sep 2012, 04:24 AM
Hi Kevin,

GridCheckBoxColumn displays a check box to represent a boolean value. You can bind this column type to a boolean field by setting its DataField property. When the grid is in browser mode, or if the column is read-only, the check box is disabled. When the column is editable, the check box is enabled. Check the following help documentation which explains more about this.
Column Types.

Thanks,
Shinu.

Tags
Grid
Asked by
Kelvin
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Kelvin
Top achievements
Rank 1
Share this question
or