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

Populating DropDownList in EditForm

4 Answers 384 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Eric Klein
Top achievements
Rank 1
Eric Klein asked on 17 May 2011, 09:31 PM
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

4 Answers, 1 is accepted

Sort by
0
saravanan k
Top achievements
Rank 1
answered on 18 May 2011, 04:42 AM
Hi Eric,

            You can bind it like this,

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

Regards,
Saravanan K
0
Princy
Top achievements
Rank 2
answered on 18 May 2011, 05:55 AM
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.
0
Eric Klein
Top achievements
Rank 1
answered on 18 May 2011, 01:45 PM
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
0
Eric Klein
Top achievements
Rank 1
answered on 18 May 2011, 03:38 PM

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
Tags
Grid
Asked by
Eric Klein
Top achievements
Rank 1
Answers by
saravanan k
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Eric Klein
Top achievements
Rank 1
Share this question
or