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

RadGrid esdatasource and dropdownlists - cannot access selected dropdown value when inserting

1 Answer 87 Views
Grid
This is a migrated thread and some comments may be shown as answers.
evo
Top achievements
Rank 1
evo asked on 07 Dec 2010, 09:23 AM

Hi,
I'm relatively new to the Radgrid and EntitySpaces. I know that they work extremely well together but I've been stumped for a few hours today with a problem. I've got a rad grid that is bound to an esDataSource, the datasource has a field that is a lookup value. I can bind the grid to the initial datasource and at this stage insertion and editing works fine. However, when I use a template column to add an dropdown control for the look up field (binding it in the ItemDataBound event handler), I can edit values well but when I insert new values though the lookup values are saved as empty values. I cant seem to grab the lookup dropdown value during the insert event.

Any suggestions would be helpful. Sample code or examples would be appriciated.

Here's my code:

<asp:Panel ID="pnlBreadcrumb" runat="server">
</asp:Panel>
  
<asp:Label ID="lblMessage" runat="server" Visible="false" Text="No list configuration found" />
  
  
<telerik:RadGrid ID="grdLevelValues" runat="server" GridLines="None" 
    AllowAutomaticInserts="True" AllowAutomaticUpdates="True" 
    DataSourceID="datListValues" AutoGenerateColumns="False" 
    onitemcommand="grdLevelValues_ItemCommand" 
    onitemdatabound="grdLevelValues_ItemDataBound" 
    onitemcreated="grdLevelValues_ItemCreated"    
    AllowSorting="True"
    AllowPaging="True" 
    PageSize="25"
    >
<HeaderContextMenu EnableAutoScroll="True"></HeaderContextMenu>
  
<MasterTableView datakeynames="IdBusinessUnit" datasourceid="datListValues" CommandItemDisplay="Top" EditMode="InPlace">
    <Columns>
        <telerik:GridBoundColumn DataField="BusinessUnitName" HeaderText="Business Unit" 
            UniqueName="column1">            
        </telerik:GridBoundColumn>                    
        <telerik:GridTemplateColumn UniqueName="State">
            <HeaderTemplate>
                <asp:Label ID="lblStateNameHeader" Text="State Name" runat="server"></asp:Label>               
            </HeaderTemplate>
            <ItemTemplate>
                <asp:Label ID="lblStateName" runat="server" Text='<%# Eval("UpToAddressStateItemByIdState.State") %>' />
            </ItemTemplate>
            <EditItemTemplate>
                <asp:DropDownList ID="ddlStateName" runat="server"></asp:DropDownList>
            </EditItemTemplate>
        </telerik:GridTemplateColumn
                                                                         
  
          
        <telerik:GridEditCommandColumn></telerik:GridEditCommandColumn>
         
    </Columns>
  
<EditFormSettings>
<EditColumn UniqueName="EditCommandColumn1"></EditColumn>
</EditFormSettings>
</MasterTableView>
</telerik:RadGrid>
  
  
  
<cc1:esDataSource ID="datListValues" runat="server" 
    onescreateentity="datListValues_esCreateEntity" 
    onesselect="datListValues_esSelect"
    AutoPaging="true" 
    AutoSorting="true"/>

code behind:
protected void Page_Load(object sender, EventArgs e)
       {
                 }
       protected void grdLevelValues_ItemCommand(object source, GridCommandEventArgs e)
       
                 }
       protected void grdLevelValues_ItemDataBound(object sender, GridItemEventArgs e)
       {
           if ((e.Item is GridEditableItem) && e.Item.IsInEditMode)
           {
                
               AddressStateCollection states = new AddressStateCollection();
               states.Query.SelectAll();
               states.Query.Load();
               GridEditableItem gridEditableItem = (GridEditableItem)e.Item;
                  
               DropDownList dropDownList = (DropDownList)gridEditableItem["State"].FindControl("ddlStateName");
               dropDownList.DataSource = states;
               dropDownList.DataTextField = "State";                
               dropDownList.DataValueField = "IdState";
                 
               //Find the selected index by assuming that the ddl datasource will maintain the order inwhich its items were added.
               int ddlSelectedIndex =0;
               foreach(AddressStateItem state in states)
               {
                  if ((gridEditableItem.DataItem is GridInsertionObject) == false)                   
                   {
                       if (state.IdState == System.Convert.ToInt32(((Westpac.VOS.Data.BusinessUnitItem)gridEditableItem.DataItem).IdState))
                       {
                           break;
                       }
                       ddlSelectedIndex ++;
                   }
                   else
                       break;
               }
               dropDownList.SelectedIndex =  ddlSelectedIndex; 
                 
               dropDownList.DataBind();
           }   
       }
       protected void grdLevelValues_ItemCreated(object sender, GridItemEventArgs e)
       {
           if (e.Item is GridEditableItem && e.Item.IsInEditMode)
           {
               GridEditableItem item = e.Item as GridEditableItem;
               //validation code goes here...
           }
       }
       protected void datListValues_esSelect(object sender, EntitySpaces.Web.esDataSourceSelectEventArgs e)
       {
           BusinessUnitCollection values = new BusinessUnitCollection();
           values.Query.SelectAll();
           values.Query.Load();
           e.Collection = values;
         
       }
       protected void datListValues_esCreateEntity(object sender, EntitySpaces.Web.esDataSourceCreateEntityEventArgs e)
       {
           BusinessUnitItem entity = new BusinessUnitItem();
           // Insert
           if (e.PrimaryKeys == null) // A new record to be created
           {
               entity.AddNew();
               entity.IdBrand = VosContext.Current.Brand.Id;
               foreach (GridDataItem item in this.grdLevelValues.Items)
               {
                   DropDownList dropDownList = (DropDownList)item["State"].FindControl("ddlStateName");
                   if (dropDownList != null)
                   {
                       entity.IdState = System.Convert.ToInt32(dropDownList.SelectedValue);
                   }
               }  
           }
           else // Update
           {                
               entity.LoadByPrimaryKey((int)e.PrimaryKeys[0]);
               
               foreach (GridDataItem item in this.grdLevelValues.Items)
               {
                    
                    DropDownList dropDownList = (DropDownList)item["State"].FindControl("ddlStateName");
                    
                    if (dropDownList != null)
                    {
                        entity.IdState = System.Convert.ToInt32(dropDownList.SelectedValue);
                    }
                 
               }  
                  
           }
           e.Entity = entity;
       }
       

1 Answer, 1 is accepted

Sort by
0
evo
Top achievements
Rank 1
answered on 08 Dec 2010, 01:47 AM

It took me some time but I've sorted it out. All I was really missing was a way to reference the template controls dropdown list.

Here's the solution :

(DropDownList)this.grdLevelValues.MasterTableView.GetInsertItem()["State"].Controls[1]).SelectedValue

Here's where I used it in code.

protected void datListValues_esCreateEntity(object sender, EntitySpaces.Web.esDataSourceCreateEntityEventArgs e)
     {
         BusinessUnitItem entity = new BusinessUnitItem();
         // Insert
         if (e.PrimaryKeys == null) // A new record to be created
         {
             entity.AddNew();
             entity.IdBrand = VosContext.Current.Brand.Id;
             
             entity.IdState = System.Convert.ToInt32(((DropDownList)this.grdLevelValues.MasterTableView.GetInsertItem()["State"].Controls[1]).SelectedValue);
         }
         else // Update
         {                
             entity.LoadByPrimaryKey((int)e.PrimaryKeys[0]);

Hope this helps someone esle.
Tags
Grid
Asked by
evo
Top achievements
Rank 1
Answers by
evo
Top achievements
Rank 1
Share this question
or