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

Loop through radgrid

3 Answers 411 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Germaine
Top achievements
Rank 1
Germaine asked on 01 Jun 2011, 07:44 PM
I have a radgrid and I need to loop through (when the user hits a button) and check if the checkbox(es) has been selected and then get the ID to save into a variable.

<telerik:RadGrid ID="RadGrid1" runat="server" AllowFilteringByColumn="True" AllowSorting="True"
                    CellSpacing="0" GridLines="None" AutoGenerateColumns="False"
                    AllowMultiRowSelection="True" AllowPaging="True" PageSize="20" >
                    <GroupingSettings CaseSensitive="false" />
                    <MasterTableView ShowFooter="false" DataKeyNames="ID" AllowFilteringByColumn="True" >
                        <Columns>
                            <telerik:GridClientSelectColumn UniqueName="chbxSelection" />
                            <telerik:GridBoundColumn UniqueName="FirstName" SortExpression="First_x0020_Name" HeaderText="First Name"
                                DataField="First_x0020_Name" AutoPostBackOnFilter="true" CurrentFilterFunction="Contains" AllowFiltering="true">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn UniqueName="LastName" SortExpression="Last_x0020_Name" HeaderText="Last Name"
                                DataField="Last_x0020_Name" AutoPostBackOnFilter="true" CurrentFilterFunction="Contains" AllowFiltering="true">
                            </telerik:GridBoundColumn>
                             <telerik:GridBoundColumn UniqueName="Organization" SortExpression="Organization" HeaderText="Organization"
                                DataField="Organization" AutoPostBackOnFilter="true" CurrentFilterFunction="Contains" AllowFiltering="true">
                            </telerik:GridBoundColumn>
                             <telerik:GridBoundColumn UniqueName="Title" SortExpression="Title" HeaderText="Title"
                                DataField="Title" AutoPostBackOnFilter="true" CurrentFilterFunction="Contains" AllowFiltering="true">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn UniqueName="ID" Visible="false" DataField="ID" AutoPostBackOnFilter="true" CurrentFilterFunction="Contains">
                            </telerik:GridBoundColumn>
                        </Columns>
                    </MasterTableView>
                    <ClientSettings>
                        <Selecting AllowRowSelect="true" />
                    </ClientSettings>
                </telerik:RadGrid>

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 02 Jun 2011, 06:12 AM
Hello Germaine,

Try the following code snippet to achieve your scenario.
C#:
protected void Button1_Click(object sender, EventArgs e)
    {
        int count = 0;
        foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
        {
            CheckBox chk = (CheckBox)item["chbxSelection"].Controls[0];
            if (chk.Checked)
            {
                count++;
            }
        }
        Response.Write(count);
    }

Thanks,
Shinu.
0
Germaine
Top achievements
Rank 1
answered on 02 Jun 2011, 06:37 AM
That code did not work.  It said the checkbox was not checked (false) even when all were checked.  Do you have any other suggestions or code snippets?

0
Shinu
Top achievements
Rank 2
answered on 03 Jun 2011, 08:40 AM
Hello Germaine,

The code worked as expected at my end and it returns the exact count of the checked items. If the problem still persist I suggest you to try to use Template column with asp:CheckBox for selection.

Grid / Server-side Row Selection.

Hope this might solves the issue.

Thanks,
Shinu.
Tags
Grid
Asked by
Germaine
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Germaine
Top achievements
Rank 1
Share this question
or