RadComboBox in GridTemplateColumn

4 posts, 1 answers
  1. oVan
    oVan avatar
    35 posts
    Member since:
    Apr 2008

    Posted 13 Mar 2009 Link to this post

    I have a GridTemplateColumn with a RadComboBox in the EditItemTemplate:
    <telerik:GridTemplateColumn DataField="Priority" DataType="System.Int32"  HeaderText="Priority" SortExpression="Priority" UniqueName="Priority"
        <EditItemTemplate> 
            <telerik:RadComboBox ID="comboPriority" runat="server"></telerik:RadComboBox> 
        </EditItemTemplate> 
    </telerik:GridTemplateColumn> 


    <asp:LinqDataSource ID="LinqTasks" runat="server" ContextTypeName="SWcom.SWDataContext" EnableDelete="True" EnableInsert="True" EnableUpdate="True" TableName="Tasks"
    </asp:LinqDataSource> 

    The combo is populated in code-behind, in the RadGrid's ItemDataBound event:
        public Dictionary<string, int> Priors = new Dictionary<string, int>() 
        { 
            { "Low",    -1     }, 
            { "Normal",  0     }, 
            { "High",    1     } 
        }; 
         
        protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
        { 
            if (e.Item is GridEditableItem && e.Item.IsInEditMode) 
            { 
                GridEditableItem eeditItem = e.Item as GridEditableItem; 
                RadComboBox combo = (RadComboBox)editItem.FindControl("comboPriority"); 
                 
                combo.DataSource = Priors
                combo.DataTextField = "Key"
                combo.DataValueField = "Value"
                combo.DataBind(); 
                combo.SelectedValue = ((SWcom.Task)editItem.DataItem).Priority.GetValueOrDefault(0).ToString(); 
            }  
        } 


    This works fine when I open a row for editing, but when I click 'update', the new selected value from the combobox is not saved in the database. I'm using a simple LinqDataSource (see above). I'm sure I have missed something obvious, but I can't find it. 

    Also, is there a more elegant or better way to get a key/value list bound to a combobox instead of a Dictionary?

    Thanks!
    oVan
  2. Answer
    Tsvetoslav
    Admin
    Tsvetoslav avatar
    1823 posts

    Posted 16 Mar 2009 Link to this post

    Hi oVan,

    Below are the steps you need to take in order to bind the combo correctly and get its value persisted to the underlying datasource:

    1. In the aspx mark-up for the combo bind its selected value property to the desired data field:

                        <telerik:GridTemplateColumn DataField="Priority" DataType="System.Int32" HeaderText="Priority" 
                            SortExpression="Priority" UniqueName="Priority">  
                            <EditItemTemplate> 
                                <telerik:RadComboBox ID="comboPriority" SelectedValue='<%#Bind("Priority") %>' runat="server">  
                                </telerik:RadComboBox> 
                            </EditItemTemplate> 
                        </telerik:GridTemplateColumn> 

    2. Remove the code for the ItemDataBound event.

    3. Wire up an event handler to the item created event.

    4. Add to the latter the code from the ItemDataBound event without calling the combo's DataBind() method and without assigning its selected value field.

            if (e.Item is GridEditableItem && e.Item.IsInEditMode)    
            {    
                GridEditableItem eeditItem = e.Item as GridEditableItem;    
                RadComboBox combo = (RadComboBox)editItem.FindControl("comboPriority");    
                    
                combo.DataSource = Priors;    
                combo.DataTextField = "Key";    
                combo.DataValueField = "Value";    
            }   
     

    Hope this helps

    Best Regards,
    Tsvetoslav
    the Telerik team


    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. oVan
    oVan avatar
    35 posts
    Member since:
    Apr 2008

    Posted 16 Mar 2009 Link to this post

    Thanks for the working answer! Apparently this works, while before I got the typical errors with null values in the database. 
  5. Lee Nessling
    Lee Nessling avatar
    9 posts
    Member since:
    Aug 2008

    Posted 03 Jun 2009 Link to this post

    Im doing the same senerio but I'm using a grid that populates via the NeedsDataSource and I'm doing a Mass Save.

    My grid contains a Usercontrol that has a combobox in it that has propertiess exposed in the UserControl and 2 other GridBoundColumns. The combobox shows correctly, but on the save, only the 2 GridBoundColumns show up in the newValues provided by ExtractValuesFromItem .

     

    <telerik:GridTemplateColumn UniqueName="Entity_ID" DataField="Entity_ID" HeaderText="Entity ID"  > 
                          
         <ItemTemplate> 
              <uc1:EditDropDown ID="EditDropDown1" runat="server" SelectedValue='<%#Bind("Entity_ID") %>'/>  
         </ItemTemplate> 
    </telerik:GridTemplateColumn> 


    ItemCreated

     void gridProvider_ItemCreated(object sender, GridItemEventArgs e)  
        {  
            if (e.Item is GridEditableItem && e.Item.IsInEditMode)  
            {  
                //first reference the edited grid item   
                GridEditableItem item = e.Item as GridEditableItem;  
     
                EditDropDown EditDropDown1 = (EditDropDown)item.FindControl("EditDropDown1");  
                EditDropDown1.DataSource = LookupDataSource;  
                EditDropDown1.DataTextField = "Lookup_Desc";  
                EditDropDown1.DataValueField = "Entity_ID";  
                 }  
        } 

    When data gets updated
     
     
    public void UpdateData()     
        {     
            foreach (GridDataItem item in gridProvider.MasterTableView.Items)     
            {     
                Hashtable newnewnewValues = new Hashtable();     
        
                //The GridTableView will fill the values from all editable columns in the hash        
                gridProvider.MasterTableView.ExtractValuesFromItem(newValues, item);     
        
                // get the key of the current row.     
                String PkFieldName = DataSource.dstAdmit_Detail.PrimaryKey[0].ColumnName;     
                Int64 PK = Convert.ToInt64(item.OwnerTableView.DataKeyValues[item.ItemIndex][PkFieldName]);     
        
                // find the related row in the dataset     
                dsAdmissionDataSet.dstAdmit_DetailRow admitDetailRow = DataSource.dstAdmit_Detail.FindByAdmit_Detail_ID(PK);     
        
                // set values based on new values     
                if (Convert.ToDateTime(newValues["From_Date"]).Date != DateTime.MinValue)     
                    admitDetailRow.From_Date = Convert.ToDateTime(newValues["From_Date"]).Date;     
        
                if (Convert.ToDateTime(newValues["Thru_Date"]).Date != DateTime.MinValue)     
                    admitDetailRow.Thru_Date = Convert.ToDateTime(newValues["Thru_Date"]).Date;     
        
                admitDetailRow.Entity_ID = Convert.ToInt64(newValues["Entity_ID"]);     
                       }     
        }    
     

Back to Top