How to bind data to radcombobox inside grid EditItemTemplate

谁说胖子不能爱 提交于 2021-01-29 15:47:28

问题


I try to bind data to a radcombobox from server side: Here is my grid:

            <telerik:RadGrid ID="gvWebUsers" runat="server" 
                OnNeedDataSource="gvWebUsers_NeedDataSource" 
                OnItemCreated="gvWebUsers_ItemCreated"
                Skin="Gray" 
                AutoGenerateColumns="false"
                OnItemDataBound="gvWebUsers_ItemDataBound" 
                AllowAutomaticUpdates="true"
                AllowPaging="true" 
                CssClass="SettingsGrid" 
                Width="99.7%">


                <MasterTableView DataKeyNames="UserID" PageSize="15" EditMode="InPlace" >
                    <Columns>


                <telerik:GridTemplateColumn HeaderText="Category" ItemStyle-Width="240px" UniqueName="Category">
                    <ItemTemplate>
                        <%#DataBinder.Eval(Container.DataItem, "IsUploadAllowed")%>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <telerik:RadComboBox runat="server" ID="RadComboBox2" skin="Gray">
                        </telerik:RadComboBox>
                    </EditItemTemplate>
                </telerik:GridTemplateColumn>

                <telerik:GridEditCommandColumn FooterText="EditCommand footer" UniqueName="EditCommandColumn"
                    HeaderText="Edit" HeaderStyle-Width="100px" UpdateText="Update">
                </telerik:GridEditCommandColumn>


                    </Columns>

                </MasterTableView>
            </telerik:RadGrid>

I have seen this code for binding in different posts and examples:

protected void gvWebUsers_ItemDataBound(object sender, GridItemEventArgs e)
{
     if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {

        GridEditableItem editItem = (GridEditableItem)e.Item;
        RadComboBox combo = (RadComboBox)editItem.FindControl("RadComboBox2");
        combo.DataSource = GetUploadStatus();
        combo.DataTextField = "Key";
        combo.DataValueField = "Value";
        combo.DataBind();
    }
}

When I run this code, it never enters inside the "if" clause. Any ideas why? It seems like I don't have " GridEditableItem ".


回答1:


The item is not GridEditableItem because it is not inside EditForm.

I used to solve this problem using:

    protected void gvWebUsers_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem myGridItem = (GridDataItem)e.Item;

            if (myGridItem.IsInEditMode)
            {
                RadComboBox combo = (RadComboBox)myGridItem["Category"].FindControl("RadComboBox2");
                combo.DataSource = GetUploadStatus();
                combo.DataTextField = "Value";
                combo.DataValueField = "Key";
                combo.DataBind();
                combo.SelectedValue = DataBinder.Eval(myGridItem.DataItem, "UploadStatus").ToString();
            }
}



回答2:


Do you have your grid bound to some data?? It won't create 'GridEditableItem's until you have data rows in your grid.



来源:https://stackoverflow.com/questions/24056323/how-to-bind-data-to-radcombobox-inside-grid-edititemtemplate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!