Populating DropDownList in EditForm

5 posts, 0 answers
  1. Eric Klein
    Eric Klein avatar
    183 posts
    Member since:
    Aug 2009

    Posted 17 May 2011 Link to this post

    Ihave a grid that is set to edit useing EditForms.  On the ItemDataBound I fill the dropdownlist in the EditForm this works great for Inserting records.  The issue is when I select and record from the grid to edit I use

    <asp:DropDownList ID="ddState" runat="server" SelectedValue='<%# Bind("StateID") %>' />
    So when I select the record the dropdownlist has not been populated yet.  How can I populate the dropdownlist so I can set the select value.

    Thanks,
    Eric
  2. saravanan k
    saravanan k avatar
    88 posts
    Member since:
    Mar 2010

    Posted 17 May 2011 Link to this post

    Hi Eric,

                You can bind it like this,

               SelectedValue='<%# DataBinder.Eval(Container, "DataItem.StateID")%>'

    Regards,
    Saravanan K
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 18 May 2011 Link to this post

    Hello Eric,

    I am not quite sure about the way you populated DropDownList. Here is the method I tried which worked fine as expected. Hope this helps you.
    C#:
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
        {
          if (e.Item is  GridEditFormItem && e.Item.IsInEditMode)
            {
             DropDownList dropdown = (e.Item as GridEditFormItem)["TemplateColumnUniqueName"].FindControl("DropDownList1") as DropDownList;
                  dropdown.DataSource =//set the datasource here
             dropdown.DataTextField = //text
             dropdown.DataBind();
               }
         }


    Another suggestion is to use Dropdown column which is available as inBuilt in RadGrid .When in browser mode, GridDropDownColumn looks and behaves like a standard GridBoundColumn. When in edit mode, however, it displays a drop-down control for each edited cell in the column. Check the following help documentation which explains more about this.
    Column types.

    Thanks,
    Princy.
  5. Eric Klein
    Eric Klein avatar
    183 posts
    Member since:
    Aug 2009

    Posted 18 May 2011 Link to this post

    ok here is my grid

    <telerik:RadGrid ID="FoundationGrid" runat="server" AllowPaging="True" AllowSorting="True"
            AutoGenerateColumns="False" GridLines="None" Skin="Sunset" OnNeedDataSource="FoundationGrid_NeedDataSource"
            OnItemDataBound="FoundationGrid_ItemDataBound" OnInsertCommand="FoundationGrid_InsertCommand">
            <PagerStyle Mode="NextPrevAndNumeric" />
            <MasterTableView DataKeyNames="FoundationID" CommandItemDisplay="Top" EditMode="EditForms"
                PageSize="25">
                <Columns>
                    <telerik:GridEditCommandColumn />
                    <telerik:GridBoundColumn DataField="FoundationID" Visible="false" DataType="System.Guid" />
                    <telerik:GridBoundColumn DataField="FoundationName" HeaderText="Foundation Name"
                        UniqueName="FoundationName" SortExpression="FoundationName" />
                    <telerik:GridCheckBoxColumn DataField="Active" HeaderText="Active" DataType="System.Boolean" />
                </Columns>
                <EditFormSettings EditFormType="Template">
                    <EditColumn UniqueName="EditCommandColumn1">
                    </EditColumn>
                    <FormTemplate>
                        <table>
                            <tr>
                                <td>
                                    Foundation Name:
                                </td>
                                <td>
                                    <asp:TextBox ID="tbName" runat="server" Width="365px" Text='<%# Bind("FoundationName") %>' />
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Address:
                                </td>
                                <td>
                                    <asp:TextBox ID="tbAddress1" runat="server" Width="365px" Text='<%# Bind("Address1") %>' />
                                </td>
                            </tr>
                            <tr>
                                <td>
                                </td>
                                <td>
                                    <asp:TextBox ID="tbAddress2" runat="server" Width="365px" Text='<%# Bind("Address2") %>' />
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    City:
                                </td>
                                <td>
                                    <asp:TextBox ID="tbCity" runat="server" Text='<%# Bind("City") %>' />
                                    State:
                                    <asp:DropDownList ID="ddState" runat="server" SelectedValue='<%# DataBinder.Eval(Container, "DataItem.StateID")%>' />
                                    Zip:
                                    <telerik:RadMaskedTextBox ID="rmtbZip" runat="server" Mask="#####-####" Text='<%# Bind("Zip") %>' />
                                </td>
                            </tr>

    and this is how I am loading the state drop down

    protected void FoundationGrid_ItemDataBound(object sender, GridItemEventArgs e)
           {
               if (e.Item is GridEditFormInsertItem && e.Item.OwnerTableView.IsItemInserted)
               {
                   GridEditFormInsertItem item = (GridEditFormInsertItem)e.Item;
                   DropDownList drList = (DropDownList)item.FindControl("ddState");
                   FoundationDataDataContext db = new FoundationDataDataContext();
                   var query = from s in db.States.OrderBy(s => s.StateText)
                               select new
                               {
                                   s.StateID,
                                   StateText = s.StateValue + ": " + s.StateText
                               };
                   drList.DataSource = query.ToList();
                   drList.DataTextField = "StateText";
                   drList.DataValueField = "StateID";
                   drList.DataBind();
                   ListItem newState = new ListItem("", "");
                   drList.Items.Insert(0, newState);
               }
           }

    This works great for insert, but when I edit the grid is not populated in time for the selectedvalue
  6. Eric Klein
    Eric Klein avatar
    183 posts
    Member since:
    Aug 2009

    Posted 18 May 2011 Link to this post

    Ok I have found one way

    protected void FoundationGrid_ItemDataBound(object sender, GridItemEventArgs e)
            {
      
      
                if ((e.Item.IsInEditMode))
                {
                    GridEditFormItem item = (GridEditFormItem)e.Item;
                    DropDownList drList = (DropDownList)item.FindControl("ddState");
      
                    FoundationDataDataContext db = new FoundationDataDataContext();
                    var query = from s in db.States.OrderBy(s => s.StateText)
                                select new
                                {
                                    s.StateID,
                                    StateText = s.StateValue + ": " + s.StateText
                                };
                    drList.DataSource = query.ToList();
                    drList.DataBind();
      
                    ListItem newState = new ListItem("", "");
                    drList.Items.Insert(0, newState);
      
                    if (e.Item is GridEditFormItem && e.Item.IsInEditMode && !e.Item.OwnerTableView.IsItemInserted)
                    {
                        Guid i = new Guid(e.Item.OwnerTableView.DataKeyValues[0]["FoundationID"].ToString());
                        Foundation nFoundation = db.Foundations.Single(f => f.FoundationID == i);
                        drList.SelectedValue = nFoundation.StateID.ToString();
                    }
                }
            }

    This seems kinda hookie, but it does work
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017