Yet Another Select Value in a ComboBox Question

4 posts, 0 answers
  1. Richard Weeks
    Richard Weeks avatar
    95 posts
    Member since:
    Oct 2009

    Posted 25 May 2010 Link to this post

    I have a normal RadGrid and in that grid columns like this:

    <telerik:GridTemplateColumn   
        HeaderText="MyColumn"   
        UniqueName="MyColumn">  
        <ItemTemplate>       
            <asp:Label ID="MyColumnLabel" runat="server" Text='<%# Eval("MyColumn") %>'></asp:Label>       
        </ItemTemplate>       
        <EditItemTemplate> 
            <telerik:RadComboBox   
                ID="Domains"   
                DataTextField="MyColumn"   
                DataValueField="MyColumnID" 
                Runat="server">  
            </telerik:RadComboBox> 
        </EditItemTemplate>       
    </telerik:GridTemplateColumn> 

    Then in the code-behind:

    protected void MyGrid_OnItemDataBound(object sender, GridItemEventArgs e)  
    {  
        if ((e.Item is GridEditableItem) && e.Item.IsInEditMode)  
        {  
            GridEditableItem gridEditableItem = (GridEditableItem)e.Item;  
     
            RadComboBox myComboBox = (RadComboBox)gridEditableItem.FindControl("MyComboBox");  
     
            this.businessData = new BusinessData();  
     
            // Don't worry about this. It pulls data from the business layer and databinds the ComboBox.  
            TelerikHelper.BindRadComboBox(myComboBox, businessdataData.Select(), false);  
        }  

    Everything works fine as is. In non-edit view, the correct value is displayed and in edit view, the combobox is displayed, populated with values.

    But I also want to set the selected item in the databound dropdown to the text value set in MyColumnLabel.

    I must be having an off-day because I simply cannot work out how to do it :(

    Can anyone enlighten me? I've been through any number of examples on the Telerik site with no luck.

    Regards,
    Richard

  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 25 May 2010 Link to this post

    Hello Richard,

    After populating RadComboBox in ItemDataBound event, you can set the SelectedValue of RadComboBox to the cell value bound for 'MyColumnLabel'. Give a try with this following code snippet.

    C#:
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
        { 
            if (e.Item is GridEditFormItem && e.Item.IsInEditMode) 
            { 
     
                GridEditFormItem edititem = (GridEditFormItem)e.Item; 
                RadComboBox dr1 = (RadComboBox)edititem.FindControl("Domains"); 
                //populate ComboBox 
                dr1.SelectedValue = ((GridEditableItem)(edititem.ParentItem))["MyColumn"].Text; 
                dr1.DataBind(); 
            } 
       } 

    Regards,
    Shinu.


  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Richard Weeks
    Richard Weeks avatar
    95 posts
    Member since:
    Oct 2009

    Posted 25 May 2010 Link to this post

    Hi, thanks for your reply.

    I think you have the correct idea but the code as is does not work.

    The .Text value comes back as an empty string but if I browse through debug to the textbox, I can see there is a value. But the code does not find it.

    Code I used:

    GridEditFormItem edititem = (GridEditFormItem)e.Item;  
     
    myComboBox.SelectedValue = ((GridEditableItem)(edititem.ParentItem))["MyColumn"].Text; 

    Very perplexing. You would surely think getting the value of a textbox in the item template when editing would be easy...

    Richard

    [edit] I forgot to mention that I need to set the selected item on the combobox by matching the text and not the value property.
  5. Richard Weeks
    Richard Weeks avatar
    95 posts
    Member since:
    Oct 2009

    Posted 25 May 2010 Link to this post

    I got it!

    GridEditFormItem item = (GridEditFormItem)e.Item;  
                      
    Label label = (Label)item.ParentItem.FindControl("MyColumnLabel");  
     
    myComboBox.FindItemByText(label.Text).Selected = true

    Thanks for the heads-up for ParentItem, that got me on the right path.

    I'm a happy man now :)

    Richard
Back to Top