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

Populate DDL inside a radgrid from sql

1 Answer 53 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Chad
Top achievements
Rank 1
Chad asked on 18 Nov 2013, 04:57 PM
Hi I was wondering how to go about populating a ddl inside a Grid Item Template Column from SQL.  I tried doing it through the On Data Bound but I could not get it to work.  Below is my code.
Thanks
    <asp:Panel ID="pnlGrid" runat="server" CssClass="panelGrid">
        <telerik:RadGrid ID="myRadGrid" runat="server" Width="90%" Skin="Web20" CssClass="dvGridWrapper" AllowSorting="true" AllowPaging="true">
                <PagerStyle AlwaysVisible="true" />
                <MasterTableView AutoGenerateColumns="false" Font-Size="10" DataKeyNames="intRecruitId">
                    <HeaderStyle ForeColor="White" Font-Bold="true" HorizontalAlign="Center" />
                    <ItemStyle HorizontalAlign="Center"/>
                    <AlternatingItemStyle BackColor="#B0C4DE"  HorizontalAlign="Center" />
                    <Columns>
                        <telerik:GridBoundColumn DataField="dtRSPStatusChanged" HeaderText="Last Changed" />
                        <telerik:GridTemplateColumn HeaderText="NAME">
                            <ItemTemplate>
                                <asp:LinkButton ID="lnkName" runat="server" Text='<%#Bind("Name")%>' CommandArgument='<%#Bind("intRecruitId")%>' CommandName="OpenForm" ToolTip="Click to See Data"></asp:LinkButton>
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridTemplateColumn HeaderText="PHASE">
                            <ItemTemplate>
                                <asp:DropDownList ID="ddlPhase" runat="server"></asp:DropDownList>
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridBoundColumn DataField="dtShip" HeaderText="SHIP DATE" />
                        <telerik:GridBoundColumn DataField="ddDate" HeaderText="DROP DEAD DATE" />
                        <telerik:GridBoundColumn DataField="dtProjReturn" HeaderText="EST RETURN DATE" />
                        <telerik:GridTemplateColumn HeaderText="STATUS">
                            <ItemTemplate>
                                <asp:DropDownList ID="ddlStatus" runat="server" OnDataBound="ddlStatus_Load"></asp:DropDownList>
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridTemplateColumn HeaderText="ISSUE">
                            <ItemTemplate>
                                <asp:DropDownList ID="ddlIssue" runat="server" OnDataBound="ddlIssue_Load"></asp:DropDownList>
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridTemplateColumn HeaderText="EDUCATION">
                            <ItemTemplate>
                                <asp:DropDownList ID="ddlEd" runat="server">
                                    <asp:ListItem Value="0" Text="N/A" />
                                    <asp:ListItem Value="1" Text="HSSR" />
                                    <asp:ListItem Value="2" Text="HSJR" />
                                    <asp:ListItem Value="3" Text="GED +" />
                                    <asp:ListItem Value="4" Text="GED" />
                                    <asp:ListItem Value="5" Text="GRAD" />
                                    <asp:ListItem Value="6" Text="COLLEGE" />
                                </asp:DropDownList>
                            </ItemTemplate>
<%--                            SelectedValue='<%#Bind("intRSPStatusID")%>'--%>
                        </telerik:GridTemplateColumn>
                        <telerik:GridTemplateColumn HeaderText="NOTES">
                                <ItemTemplate>
<%--                                    <asp:Image ID="imgLooker" runat="server" ImageUrl="~/Images/ViewResources.gif" ImageAlign="Middle" BorderStyle="None" />
                                    <asp:PopupControlExtender ID="popInfo" runat="server" PopupControlID="pnlPop" TargetControlID="imgLooker" DynamicContextKey='<% #Eval("strNotes")%>' 
                                    DynamicControlID="pnlpop" DynamicServiceMethod="GetNotes" Position="bottom"></asp:PopupControlExtender>--%>
                                </ItemTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridBoundColumn DataField="strRSPSiteLocation" HeaderText="RSP SITE" />
                        <telerik:GridBoundColumn DataField="strUIC" HeaderText="UIC" />
                        <telerik:GridTemplateColumn HeaderText="UPDATE">
                            <ItemTemplate>
                                <asp:LinkButton ID="lnkUpdate" runat="server" Text="Update" ToolTip="Click to Save Data"></asp:LinkButton>
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>                            
                    </Columns>
                </MasterTableView>
        </telerik:RadGrid>
    </asp:Panel>
</asp:Content>

Code Behind
    Protected Sub ddlIssue_Load(sender As Object, e As EventArgsHandles ddlIssue.Load
 
        sql = "Select strRSPIssueDesc from RSPIssue"
 
        ddlIssue.Items.Add(New ListItem("Select One""0"))
        buildDD(sql, ddlIssue)
    End Sub
 
    Protected Sub ddlSite_Load(sender As Object, e As EventArgsHandles ddlSite.Load
 
        sql = "select strRSPSiteLocation from RSPSite"
 
        ddlSite.Items.Add(New ListItem("Select One""0"))
        buildDD(sql, ddlSite)
    End Sub
 
    Protected Sub ddlStatus_Load(sender As Object, e As EventArgsHandles ddlStatus.Load
 
        sql = "Select strStatusType from RSPStatus"
 
        ddlStatus.Items.Add(New ListItem("Select One""0"))
        buildDD(sql, ddlStatus)
    End Sub

1 Answer, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 19 Nov 2013, 06:59 AM
Hi Chad,

You can use the ItemDataBound event to populate a DropDownList in RadGrid.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" OnItemDataBound="RadGrid1_ItemDataBound">
    <MasterTableView >
        <Columns>         
            <telerik:GridTemplateColumn>
                <ItemTemplate>
                    <asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="true">
                    </asp:DropDownList>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

VB:
Protected Sub RadGrid1_ItemDataBound(sender As Object, e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound
  
  If TypeOf e.Item Is GridDataItem Then
    Dim sql As String = "your query"
    Dim data As GridDataItem = DirectCast(e.Item, GridDataItem)
    Dim droplist1 As DropDownList = DirectCast(data.FindControl("DropDownList1"), DropDownList)
    droplist1.Items.Add(New ListItem("Select One", "0"))
    droplist1.DataSource = BuildDD(sql)
    droplist1.DataTextField = "DataFieldName"
    droplist1.DataValueField = "DataFieldName"
    droplist1.DataBind()
  End If
  
End Sub

Thanks,
Princy
Tags
Grid
Asked by
Chad
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or