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

Yet Another Select Value in a ComboBox Question

3 Answers 111 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Richard Weeks
Top achievements
Rank 2
Richard Weeks asked on 25 May 2010, 06:09 AM

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

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 25 May 2010, 08:51 AM
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.


0
Richard Weeks
Top achievements
Rank 2
answered on 26 May 2010, 12:39 AM
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.
0
Richard Weeks
Top achievements
Rank 2
answered on 26 May 2010, 01:06 AM
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
Tags
Grid
Asked by
Richard Weeks
Top achievements
Rank 2
Answers by
Shinu
Top achievements
Rank 2
Richard Weeks
Top achievements
Rank 2
Share this question
or