How to loop through radgrid and get results from radiobuttonlist

4 posts, 0 answers
  1. Martin
    Martin avatar
    2 posts
    Member since:
    Jun 2008

    Posted 06 Nov 2012 Link to this post

    I have a radgrid control with 2 radiobuttonlist.  The user will select the radiobuttonlists for all rows of the radgrid and then hit a submit button.  What I would like to do is loop through each row of the grid and find out which radio button was selected for each of the 2 radiobuttonlist and then read the value of the pid column from that row, then fire a sql update statement to update the database according to the pid and radiobutton selected.  Can someone shed some light on how to do this?  Thanks in advance.

    <telerik:RadGrid ID="RgdPub" runat="server"
            AutoGenerateColumns="false" DataSourceID="SqlRgdPub" Skin="Web20" AllowSorting="true">
            <FilterMenu EnableImageSprites="False">
            </FilterMenu>
            <ClientSettings>
                <Selecting CellSelectionMode="None" />
            </ClientSettings>
            <MasterTableView CommandItemDisplay="Top" GridLines="Both" Width="100%" AllowSorting="true" >
                            <CommandItemSettings ShowAddNewRecordButton="false"
                                ShowExportToCsvButton="true" ShowExportToExcelButton="true"
                                ShowExportToPdfButton="false" ShowExportToWordButton="true" />
                            <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column"
                                Visible="True">
                            </RowIndicatorColumn>
                            <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column"
                                Visible="True">
                            </ExpandCollapseColumn>
                            <Columns>
                                    <telerik:GridTemplateColumn HeaderText="Is this your publication?" UniqueName="Pub">
                                        <ItemTemplate>
                                        <asp:RadioButtonList ID="RblPub" runat="server" RepeatColumns=2 RepeatDirection="Horizontal">
                                            <asp:ListItem Text="Yes" Value=1></asp:ListItem>
                                            <asp:ListItem Text="No" Value=0></asp:ListItem>
                                        </asp:RadioButtonList>
                                        </ItemTemplate>
                                    </telerik:GridTemplateColumn>
                                    <telerik:GridTemplateColumn HeaderText="Is this institute supported?" UniqueName="InsPub">
                                        <ItemTemplate>
                                        <asp:RadioButtonList ID="RblInsPub" runat="server" RepeatColumns=2 RepeatDirection="Horizontal">
                                            <asp:ListItem Text="Yes" Value=1></asp:ListItem>
                                            <asp:ListItem Text="No" Value=0></asp:ListItem>
                                        </asp:RadioButtonList>
                                        </ItemTemplate>
                                    </telerik:GridTemplateColumn>
                                    <telerik:GridBoundColumn UniqueName="lastname" EditFormColumnIndex="0" HeaderText="Last Name"
                                        DataField="lastname">
                                    </telerik:GridBoundColumn>
                                    <telerik:GridBoundColumn UniqueName="firstname" EditFormColumnIndex="0" HeaderText="First Name"
                                        DataField="firstname">
                                    </telerik:GridBoundColumn>
                                    <telerik:GridBoundColumn UniqueName="institution" EditFormColumnIndex="0" HeaderText="Institution"
                                        DataField="institution">
                                    </telerik:GridBoundColumn>
                                    <telerik:GridBoundColumn UniqueName="pid" EditFormColumnIndex="0" HeaderText="PID"
                                        DataField="pid">
                                    </telerik:GridBoundColumn>
                </Columns>
                <EditFormSettings>
                    <EditColumn FilterControlAltText="Filter EditCommandColumn column">
                    </EditColumn>
                </EditFormSettings>
            </MasterTableView>
        </telerik:RadGrid>
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 06 Nov 2012 Link to this post

    Hi,

    Please take a look into the following code snippet I tried to loop through the rows and get the pid of the row in which the two RadioButtonLists are selected

    C#:
    protected void submit_Click(object sender, EventArgs e)
    {
        foreach(GridDataItem item in RadGrid1.MasterTableView.Items)
        {
            RadioButtonList RadioButtonList1 = (RadioButtonList)item.FindControl("RblPub");          
            RadioButtonList RadioButtonList2 = (RadioButtonList)item.FindControl("RblInsPub");
            if (RadioButtonList1.SelectedItem != null && RadioButtonList2.SelectedItem != null)
            {
                string id = item["pid"].Text; //get the pid here
            }
        }
    }

    Thanks,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Balakrishna Reddy
    Balakrishna Reddy avatar
    37 posts
    Member since:
    Oct 2012

    Posted 28 Jan 2013 Link to this post

    Hi,
       i have radgrid with template column which have radio button list.
    i want to bind that radiobuttonlist dynamically.my code is like bellow,i am getting null when i am trying to find the control of radio button list.
    So please help in getting of this...

    <telerik:RadGrid ID="RadGrid_Machine" runat="server" AllowPaging="True" AutoGenerateColumns="False"
                CellSpacing="0" GridLines="None" OnDeleteCommand="RadGrid_Machine_DeleteCommand"
                PageSize="5" OnInsertCommand="RadGrid_Machine_InsertCommand" OnItemCommand="RadGrid_Machine_ItemCommand"
                OnUpdateCommand="RadGrid_Machine_UpdateCommand"
                OnItemDataBound="RadGrid_Machine_ItemDataBound"
                onitemcreated="RadGrid_Machine_ItemCreated">
                <PagerStyle Mode="NextPrevAndNumeric" />
                <MasterTableView CommandItemDisplay="Top" EditMode="EditForms">
                    <Columns>
                        <telerik:GridEditCommandColumn FilterControlAltText="Filter EditCommandColumn column"
                            ButtonType="ImageButton">
                        </telerik:GridEditCommandColumn>
                        <telerik:GridBoundColumn DataField="MachineID" ReadOnly="true" HeaderText="MachineID"
                            UniqueName="MachineID">
                        </telerik:GridBoundColumn>
                        <telerik:GridDropDownColumn HeaderText="ProductType" DataField="ProductType" UniqueName="ProductType"
                            ListDataMember="ProductTypeMaster" ListTextField="ProductType">
                        </telerik:GridDropDownColumn>
                        <telerik:GridBoundColumn DataField="MachineName" FilterControlAltText="Filter column1 column"
                            HeaderText="MachineName" UniqueName="MachineName">
                        </telerik:GridBoundColumn>
                        <telerik:GridCheckBoxColumn DataField="ProcessColor" FilterControlAltText="Filter ProcessColor column"
                            HeaderText="ProcessColor" UniqueName="ProcessColor">
                        </telerik:GridCheckBoxColumn>
                        <telerik:GridCheckBoxColumn DataField="PantoneColor" FilterControlAltText="Filter PantoneColor column"
                            HeaderText="PantoneColor" UniqueName="PantoneColor">
                        </telerik:GridCheckBoxColumn>
                        <telerik:GridCheckBoxColumn DataField="MetallicColor" FilterControlAltText="Filter MetalicColor column"
                            HeaderText="MetallicColor" UniqueName="MetallicColor">
                        </telerik:GridCheckBoxColumn>
                        <telerik:GridCheckBoxColumn DataField="One23Color" FilterControlAltText="Filter One23Color column"
                            HeaderText="One23Color" UniqueName="One23Color">
                        </telerik:GridCheckBoxColumn>
                        <telerik:GridBoundColumn DataField="AvgSpeedPhr" FilterControlAltText="Filter AvgSpeedPhr column"
                            HeaderText="AvgSpeedPhr" UniqueName="AvgSpeedPhr">
                        </telerik:GridBoundColumn>
                        <telerik:GridDropDownColumn HeaderText="UnitType" DataField="UnitType" UniqueName="UnitType"
                            ListDataMember="UnitTypeMaster" ListTextField="UnitType">
                        </telerik:GridDropDownColumn><%--
                        <telerik:GridBoundColumn DataField="AvgSpeedPmin" FilterControlAltText="Filter AvgSpeedPmin column"
                            HeaderText="AvgSpeedPmin" UniqueName="AvgSpeedPmin">
                        </telerik:GridBoundColumn>--%>
                        <telerik:GridBoundColumn DataField="MakeReadyCost" FilterControlAltText="Filter MakeReadyCost column"
                            HeaderText="MakeReadyCost" UniqueName="MakeReadyCost">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="MinCost" FilterControlAltText="Filter MakeReadyCost column"
                            HeaderText="MinCost" UniqueName="MinCost">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="MakeReadyTime" FilterControlAltText="Filter MakeReadyTime column"
                            HeaderText="MakeReadyTime" UniqueName="MakeReadyTime">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="WashUpCharges" FilterControlAltText="Filter WashUpCharges column"
                            HeaderText="WashUpCharges" UniqueName="WashUpCharges">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="ChangeOverTime" FilterControlAltText="Filter ChangeOverTime column"
                            HeaderText="ChangeOverTime" UniqueName="ChangeOverTime">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="MaxPaperSize_Height" FilterControlAltText="Filter MaxPaperSize column"
                            HeaderText="MaxPaperSize_Height" UniqueName="MaxPaperSize_Height">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="MaxPaperSize_Width" FilterControlAltText="Filter MaxPaperSize column"
                            HeaderText="MaxPaperSize_Width" UniqueName="MaxPaperSize_Width">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="MinPaperSize_Height" FilterControlAltText="Filter MinPaperSize column"
                            HeaderText="MinPaperSize_Height" UniqueName="MinPaperSize_Height">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="MinPaperSize_Width" FilterControlAltText="Filter MinPaperSize column"
                            HeaderText="MinPaperSize_Height" UniqueName="MinPaperSize_Height">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="MaxPrintArea_Height" FilterControlAltText="Filter MaxPrintArea column"
                            HeaderText="MaxPrintArea_Height" UniqueName="MaxPrintArea_Height">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="MaxPrintArea_Width" FilterControlAltText="Filter MaxPrintArea column"
                            HeaderText="MaxPrintArea_Width" UniqueName="MaxPrintArea_Width">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="MinPrintArea_Height" FilterControlAltText="Filter MinPrintArea column"
                            HeaderText="MinPrintArea_Height" UniqueName="MinPrintArea_Height">
                        </telerik:GridBoundColumn>
                         <telerik:GridBoundColumn DataField="MinPrintArea_Width" FilterControlAltText="Filter MinPrintArea column"
                            HeaderText="MinPrintArea_Width" UniqueName="MinPrintArea_Width">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="Gripper" FilterControlAltText="Filter Gripper column"
                            HeaderText="Gripper" UniqueName="Gripper">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="Spoilage" FilterControlAltText="Filter Spoilage column"
                            HeaderText="Spoil Age" UniqueName="Spoilage">
                        </telerik:GridBoundColumn><%--
                        <telerik:GridBoundColumn DataField="PrintEdge" FilterControlAltText="Filter Spoilage column"
                            HeaderText="PrintEdge" UniqueName="PrintEdge">
                        </telerik:GridBoundColumn>--%>
                        <telerik:GridTemplateColumn HeaderText="PrintEdge">
                            <ItemTemplate>
                                <asp:RadioButtonList ID="rblList" runat="server" AutoPostBack="true" DataTextField="PrintEdge" RepeatColumns="3" DataValueField="PrintEdge" DataMember="MachineMaster" RepeatDirection="Horizontal">
                                    <asp:ListItem Text="Short" Value="0" Selected="True">a</asp:ListItem>
                                    <asp:ListItem Text="Long" Value="1">b</asp:ListItem>
                                    <asp:ListItem Text="Both" Value="2">c</asp:ListItem>
                                </asp:RadioButtonList>
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridCheckBoxColumn FilterControlAltText="Filter IsActive column" UniqueName="IsActive"
                            DataField="IsActive" HeaderText="IsActive">
                        </telerik:GridCheckBoxColumn>
                        <telerik:GridButtonColumn FilterControlAltText="Filter column19 column" UniqueName="column19"
                            ButtonType="ImageButton" CommandName="Delete">
                        </telerik:GridButtonColumn>
                    </Columns>
                    <EditFormSettings>
                        <EditColumn ButtonType="ImageButton">
                        </EditColumn>
                    </EditFormSettings>
                </MasterTableView>
                <FilterMenu EnableImageSprites="False">
                </FilterMenu>
                <HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Default">
                </HeaderContextMenu>
                <ClientSettings>
                    <Scrolling AllowScroll="true" />
                </ClientSettings>
            </telerik:RadGrid>

    *******************************************************************************
    protected void RadGrid_Machine_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item.IsInEditMode)
            {
                GridEditableItem editItem = (GridEditableItem)e.Item;
                GridEditManager editMgr = (GridEditManager)editItem.EditManager;

                GridDropDownListColumnEditor editor = (GridDropDownListColumnEditor)editMgr.GetColumnEditor("ProductType");
                GridDropDownListColumnEditor editor1 = (GridDropDownListColumnEditor)editMgr.GetColumnEditor("UnitType");
                RadComboBox rcbProductType = editor.ComboBoxControl;
                RadComboBox rcbUnitType = editor1.ComboBoxControl;

                    DataTable dtProduct = objProductType.GetAllProductTypeMaster();
                    objProductType.FillFilteredRadComboList(rcbProductType, dtProduct);

                    rcbUnitType.Items.Clear();

                    RadioButtonList list = (RadioButtonList)editItem.FindControl("rblList");   //Here i am getting null
                    RadioButtonList rblAnswer = editItem.FindControl("rblList") as RadioButtonList;      //Here i am getting null
         
            }
        }
  5. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 28 Jan 2013 Link to this post

    Hi Balakrishna Reddy,

    The RadioButtonList is kept inside the ItemTemplate of the GridTemplateColumn, that is why you are getting null while accessing the RadioButtonList in edit mode. You will be able to access the RadioButtonList in the view mode as follows.

    C#:
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
            if (e.Item is GridDataItem)
            {
                GridDataItem ditem = (GridDataItem)e.Item;
                RadioButtonList list = (RadioButtonList)ditem.FindControl("rblList");
            }
    }

    Thanks.
    Shinu.

Back to Top