problem with combo box in a grid

4 posts, 0 answers
  1. mww
    mww avatar
    257 posts
    Member since:
    Sep 2012

    Posted 07 Oct 2009 Link to this post

    I have a combo box in my grid, when I edit the underlying data I want the current data item to be selected in the combo box

    <telerik:GridTemplateColumn UniqueName="CastMember" HeaderText="Cast Member" DataField="CastMemberName" 
                            SortExpression="CastMember" ItemStyle-HorizontalAlign="Left">  
                              
                            <ItemTemplate> 
                                <%#DataBinder.Eval(Container.DataItem, "CastMemberName")%> 
                            </ItemTemplate> 
                            <EditItemTemplate> 
                                <telerik:RadComboBox DataTextField="CastMemberName" Skin="Vista" 
                                    DataValueField="CastMemberName"   
                                    ID="RadComboBoxCastMember"   
                                    runat="server"   
                                    Width="220px" > 
                                </telerik:RadComboBox> 
                            </EditItemTemplate> 
     
    <ItemStyle HorizontalAlign="Left"></ItemStyle> 
                        </telerik:GridTemplateColumn> 

    in my codebehind I have this

    protected void RadGridCastList_ItemCreated(object sender, GridItemEventArgs e)  
            {  
                if (e.Item is GridDataItem)  
                {  
                    if (e.Item.IsInEditMode)  
                    {  
                        GridEditableItem editItem = e.Item as GridEditableItem;  
                        RadComboBox combo = (RadComboBox)editItem.FindControl("RadComboBoxCastMember");  
                        combo.DataTextField = "CastMemberName";  
                        combo.DataValueField = "CastMemberName";  
                        combo.DataSource = castmanager.CastNames();  - a list of cast member names
                        combo.DataBind();  
                        try  
                        {  
                            combo.FindItemByValue(DataBinder.Eval(e.Item.DataItem, "CastMemberName").ToString()).Selected = true;  
                        }  
                        catch (NullReferenceException ex)  
                        {  
                            combo.SelectedIndex = 0;  
                        }  
     
                        RadNumericTextBox rntb = (RadNumericTextBox)editItem.FindControl("RadNumericTextBoxDisplayOrder");  
                        try  
                        {  
                            rntb.DbValue = (int)DataBinder.Eval(e.Item.DataItem, "DisplayOrder");  
                        }  
                        catch (Exception ex2)  
                        {  
                            //  
                        }  
                    }  
                    else  
                    {  
     
                    }  
                }  
                  
     
            } 


    this line
    combo.FindItemByValue(DataBinder.Eval(e.Item.DataItem, "CastMemberName").ToString()).Selected = true;

    should select the current name in the combo, but it doesnt.  The first item in the the combo is always set.
    Ive used this method many, many times before and its always worked, yet in this case it wont !

    Ive checked the value in CastMemberName and its a valid name that is in  the data bound to the combo.
    Can anyone see what the problem is ?
  2. Pavlina
    Admin
    Pavlina avatar
    6084 posts

    Posted 07 Oct 2009 Link to this post

    Hello,

    You can use numerous different instances of RadComboBox inside RadGrid. Furthermore, you can load the combobox items on demand. You can also preselect the value to be shown in the combobox when the RadGrid's template is in an edit mode. You can directly bind the SelectedValue inline.
    For a live example please review the following link:
    Combo in Grid

    Greetings,
    Pavlina
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. mww
    mww avatar
    257 posts
    Member since:
    Sep 2012

    Posted 07 Oct 2009 Link to this post

    thanks, but that doesnt answer my question at all.  Ive used code in the ItemCreated event and want to know why this line of code wont work

    combo.FindItemByValue(DataBinder.Eval(e.Item.DataItem, "CastMemberName").ToString()).Selected = true;

    can you see why it isnt working ?
  5. Parth
    Parth avatar
    8 posts
    Member since:
    Aug 2009

    Posted 07 Oct 2009 Link to this post

    Hi,

    I use this piece of code to set the combo value, without the try catch block you have.   This may solve your problem, you can pass in the combo and value.

     
    private void SetCombotoValue(Telerik.Web.UI.RadComboBox drpList, string dbValue)  
            {  
                Telerik.Web.UI.RadComboBox l_drpLst = drpList;  
                if (dbValue != null)  
                {  
                    Telerik.Web.UI.RadComboBoxItem itemToSelect = l_drpLst.Items.FindItemByValue(dbValue);  
                    if (itemToSelect != null)  
                    {  
                        l_drpLst.SelectedIndex = l_drpLst.Items.IndexOf(itemToSelect);  
                    }  
                    else 
                        l_drpLst.SelectedIndex = 0;  
                }  
                else 
                    l_drpLst.SelectedIndex = 0;  
     
            } 

    Thanks,
    Parth
Back to Top