Get Comobbox at code behind in grid

2 posts, 0 answers
  1. Kishor Dalwadi
    Kishor Dalwadi avatar
    40 posts
    Member since:
    Nov 2009

    Posted 09 Aug 2010 Link to this post

    Hello Sir, I am Using Combobox in RadGrid Edit Tempate.

    Code is Given Below.

    I want combobox for insert and set value from codebehind.

    And SelecetedValue is not woking in edittemplate.

    Please Give me proper solution.

    ---- ASPX Code ---
    <telerik:RadGrid ID="gridPostCode" runat="server" Width="96%" GridLines="None" AutoGenerateColumns="False"
            PageSize="10" AllowSorting="True" AllowPaging="True" OnUpdateCommand="gridPostCode_UpdateCommand"
            OnNeedDataSource="gridPostCode_NeedDataSource" ShowStatusBar="true" OnInsertCommand="gridPostCode_InsertCommand"
            OnDeleteCommand="gridPostCode_DeleteCommand"
            onitemcommand="gridPostCode_ItemCommand">
            <MasterTableView DataKeyNames="Id" Width="100%" CommandItemDisplay="Top" EditMode="InPlace">
                <Columns>
                    <telerik:GridBoundColumn UniqueName="Id" HeaderText="Id" DataField="Id" ReadOnly="true" />
                    <telerik:GridTemplateColumn UniqueName="TemplateColumn" HeaderText="Product Name"
                        SortExpression="Product Name">
                        <ItemTemplate>
                            <%#DataBinder.Eval(Container.DataItem,"ProductId")%>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <telerik:RadComboBox OnItemsRequested="cmbProducts_ItemsRequested" EnableLoadOnDemand="True"
                                ID="cmbProducts" runat="server" Height="140px" Width="150px" >
                            </telerik:RadComboBox>
                        </EditItemTemplate>
                    </telerik:GridTemplateColumn>              
                    <telerik:GridEditCommandColumn ButtonType="ImageButton" EditImageUrl="~/Images/edit-icon.png"
                        UpdateImageUrl="~/Images/edit-icon.png" UniqueName="EditCommandColumn" HeaderText="Modify"
                        HeaderStyle-HorizontalAlign="Center" CancelImageUrl="~/Images/delete-icon.png">
                        <ItemStyle Width="50px" HorizontalAlign="Center" />
                    </telerik:GridEditCommandColumn>               
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>

    -- CS Code--
     protected void gridPostCode_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
                GridEditableItem item = e.Item as GridEditableItem;
                RadComboBox ddl = (RadComboBox)item.FindControl("cmbProducts");
            }
        }

    Thanks.
    Kishor Dalwadi
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 09 Aug 2010 Link to this post

    Hi Kishor,

    Since the ItemCommand is too early for rendering the editform controls, you need to use the code in ItemDataBound event instead.

    Code sample:
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            GridEditableItem editItem = (GridEditableItem)e.Item;
            RadComboBox combo = (RadComboBox)editItem.FindControl("cmbProducts");
            // Set the SelectedValue for combo
        }
    }


    -Shinu.
Back to Top