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

problem with combo box in a grid

3 Answers 85 Views
Grid
This is a migrated thread and some comments may be shown as answers.
mww
Top achievements
Rank 1
mww asked on 07 Oct 2009, 02:13 PM
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 ?

3 Answers, 1 is accepted

Sort by
0
Pavlina
Telerik team
answered on 07 Oct 2009, 03:03 PM
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.
0
mww
Top achievements
Rank 1
answered on 07 Oct 2009, 03:13 PM
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 ?
0
Parth
Top achievements
Rank 1
answered on 07 Oct 2009, 05:20 PM
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
Tags
Grid
Asked by
mww
Top achievements
Rank 1
Answers by
Pavlina
Telerik team
mww
Top achievements
Rank 1
Parth
Top achievements
Rank 1
Share this question
or