How To Bind ASP dropdown in RAdGrid

1 posts, 0 answers
  1. Perry
    Perry avatar
    62 posts
    Member since:
    Jul 2015

    Posted 14 Sep 2017 Link to this post

    I have this RAD Grid

     

    <telerik:RadGrid ID="gvMembers" runat="server"
                AutoGenerateColumns="false"
                OnNeedDataSource="gvMembers_NeedDataSource"
                AllowSorting="true"
                AllowPaging="true"
                PageSize="10"
                AllowAutomaticUpdates="true"
                AllowAutomaticInserts="true"
                AllowAutomaticDeletes="true"
                OnItemCreated="gvMembers_ItemCreated"
                OnItemInserted="gvMembers_ItemInserted"
                OnPreRender="gvMembers_PreRender"
                OnInsertCommand="gvMembers_InsertCommand"
                OnItemDataBound="gvMembers_ItemDataBound"
                OnUpdateCommand="gvMembers_UpdateCommand"
                OnDeleteCommand="gvMembers_DeleteCommand"
     
                >
                <HeaderStyle CssClass="GridHeader" />
                <PagerStyle Mode="NextPrevNumericAndAdvanced" />
                <MasterTableView AutoGenerateColumns="false" DataKeyNames="UserID" CommandItemDisplay="Top" InsertItemPageIndexAction="ShowItemOnCurrentPage">
                    <Columns>
                        <telerik:GridEditCommandColumn></telerik:GridEditCommandColumn>
                        <telerik:GridBoundColumn DataField="UserID" HeaderText="UserID" ReadOnly="true" UniqueName="UserID"></telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="FirstName" HeaderText="First Name" SortExpression="FirstName" UniqueName="firstname"></telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="FirstName" HeaderText="Last Name" SortExpression="LastName" UniqueName="lastname"></telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="UserName" HeaderText="User Name" SortExpression="UserName" UniqueName="username"></telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="EmailAddress" HeaderText="Email" SortExpression="EmailAddress" UniqueName="email"></telerik:GridBoundColumn>
     
                        <telerik:GridTemplateColumn UniqueName="TemplateColumn" HeaderText="Role">
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server"
                                    Text='<%# DataBinder.Eval(Container.DataItem, "Role") %>'>
                                </asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:DropDownList ID="ddlRole" runat="server" DataTextField="Role1" DataValueField="RoleID"></asp:DropDownList>
                            </EditItemTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridButtonColumn ConfirmText="Delete this Member?" ConfirmDialogType="RadWindow"
                            ConfirmTitle="Delete" ButtonType="PushButton" Text="Delete" CommandName="Delete" />
                    </Columns>
                </MasterTableView>
            </telerik:RadGrid>

     

    I need to bind the asp dropdown list for my Update and Insert.

    I tried this C# code:

    protected void gvMembers_ItemDataBound(object sender, GridItemEventArgs e)
            {
     
                DropDownList ddl = (DropDownList)e.Item.FindControl("ddlRole");
                ddl.SelectedValue = (string)DataBinder.Eval(e.Item.DataItem, "Role1").ToString();
     
            }

     

    But it gets an error message {"Object reference not set to an instance of an object."} when I try to view the web page.

     

    What is the proper procedure to accomplish this?

Back to Top