This is a migrated thread and some comments may be shown as answers.
Error Attempting To Bind RadComboBox in GridTemplateColumn in Code Behind
1 Answer 43 Views
This is a migrated thread and some comments may be shown as answers.
amonte
Top achievements
Rank 1
amonte asked on 17 Oct 2012, 04:15 PM
I am attempting to load values in a RadComboBox inside a RadGrid and I am getting the following error -

"Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control"

Here is the markup for the grid

<telerik:RadGrid runat="server" ID="rgrdClaimantStatus" 
    AutoGenerateColumns="False" 
    Skin="Windows7"  OnInt="InitializeGrid"
    Style="margin-top: 0px"
    OnNeedDataSource="rgrdClaimantStatus_NeedDataSource" 
    OnUpdateCommand="rgrdClaimantStatus_UpdateCommand"
    OnInsertCommand="rgrdClaimantStatus_InsertCommand" 
    OnDeleteCommand="rgrdClaimantStatus_DeleteCommand"
    OnItemCommand="rgrdClaimantStatus_ItemCommand"
    OnItemDataBound="rgrdClaimantStatus_ItemDataBound"   
    OnPreRender="rgrdClaimantStatus_PreRender" 
    OnItemCreated="rgrdClaimantStatus_ItemCreated"
    TabIndex="21"              
    >
    <MasterTableView commanditemdisplay="Top" EditMode="InPlace" ShowHeadersWhenNoRecords="True"
        <Columns>
            <telerik:GridButtonColumn CommandName="Delete" ButtonType="ImageButton" ImageUrl="~/PPSPortal/images/Cancel.gif" UniqueName="DeleteColumn" ShowInEditForm="True" />
            <telerik:GridTemplateColumn UniqueName="StatusDesc" DataField="StatusDesc" HeaderText="Status" FooterStyle-Font-Bold="true" FooterStyle-Wrap="false" ItemStyle-Wrap="false">
                <ItemTemplate>                                       
                    <telerik:RadComboBox runat="server" ID="rcbClaimantStatusCode" Width="100"  Text='<%# Bind("StatusDesc")  %>'  />
                </ItemTemplate>                
            </telerik:GridTemplateColumn>
            <telerik:GridTemplateColumn UniqueName="DateFrom" DataField="DateFrom" HeaderText="Date From" FooterStyle-Font-Bold="true" FooterStyle-Wrap="false" ItemStyle-Wrap="false">
                <ItemTemplate>                                       
                    <telerik:RadDatePicker runat="server" ID="rdpClaimantStatusStartDate" width="150" DbSelectedDate='<%# Bind("DateFrom") %>'>
                        <Calendar UseRowHeadersAsSelectors="False" UseColumnHeadersAsSelectors="False" ViewSelectorText="x"></Calendar>
                        <DateInput DisplayDateFormat="M/d/yyyy" DateFormat="M/d/yyyy" TabIndex="2" />
                        <DatePopupButton ImageUrl="" HoverImageUrl="" TabIndex="2"></DatePopupButton>
                    </telerik:RadDatePicker
                </ItemTemplate>                
            </telerik:GridTemplateColumn>
            <telerik:GridTemplateColumn UniqueName="DateTo" DataField="DateTo" HeaderText="Date To" FooterStyle-Font-Bold="true" FooterStyle-Wrap="false" ItemStyle-Wrap="false">
                <ItemTemplate>                                       
                    <telerik:RadDatePicker runat="server" ID="rdpClaimantEndDate" width="150" onkeydown="ClaimantStatusEndDateKeyDown(this, event)" DbSelectedDate='<%# Bind("DateTo")  %>'>
                        <Calendar UseRowHeadersAsSelectors="False" UseColumnHeadersAsSelectors="False" ViewSelectorText="x"></Calendar>
                        <DateInput DisplayDateFormat="M/d/yyyy" DateFormat="M/d/yyyy" TabIndex="2" />
                        <DatePopupButton ImageUrl="" HoverImageUrl="" TabIndex="2"></DatePopupButton>
                    </telerik:RadDatePicker
                </ItemTemplate>                
            </telerik:GridTemplateColumn>
            <telerik:GridTemplateColumn UniqueName="DateCreated" DataField="DateCreated" HeaderText="Date Created" FooterStyle-Font-Bold="true" FooterStyle-Wrap="false" ItemStyle-Wrap="false">
                <ItemTemplate>                                       
                     <asp:Label runat="server" ID="lblClaimantDateCreated" Width="150" Text='<%# Bind("DateCreated")  %>' />
                </ItemTemplate>                
            </telerik:GridTemplateColumn>                                                            
            <telerik:GridTemplateColumn UniqueName="DateEdited" DataField="DateEdited" HeaderText="Date Edited" FooterStyle-Font-Bold="true" FooterStyle-Wrap="false" ItemStyle-Wrap="false">
                <ItemTemplate>                                       
                     <asp:Label runat="server" ID="lblClaimantDateEdited" Width="150" Text='<%# Bind("DateEdited")  %>' />
                </ItemTemplate>                
            </telerik:GridTemplateColumn>            
            <telerik:GridTemplateColumn UniqueName="ModifiedBy" DataField="ModifiedBy" HeaderText="Modified By" FooterStyle-Font-Bold="true" FooterStyle-Wrap="false" ItemStyle-Wrap="false">
                <ItemTemplate>                                       
                     <asp:Label runat="server" ID="lblClaimantModifiedBy" Width="150" Text='<%# Bind("ModifiedBy")  %>' />
                </ItemTemplate>                
            </telerik:GridTemplateColumn>                                                                      
        </Columns>
    </MasterTableView>
    <ClientSettings>              
        <Selecting AllowRowSelect="True" EnableDragToSelectRows="false" />
        <DataBinding EnableCaching="True" />
        <ClientEvents OnKeyPress="KeyPressed" />
    </ClientSettings>               
</telerik:RadGrid>

and here is the code that is binding the combo box

protected void rgrdClaimantStatus_ItemDataBound(object source, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        //SetEditModeColumnWidths(e.Item as GridDataItem);
        RadComboBox status = ((e.Item as GridDataItem)["StatusDesc"].FindControl("rcbClaimantStatusCode") as RadComboBox);
        LoadClaimantStatusList(status, Convert.ToInt32((GetSelectedCompany())));
      
    }
}

protected void LoadClaimantStatusList(RadComboBox rddlStatus, int companyId)
{
    ReferenceProcess rc = new ReferenceProcess();
    Dictionary<int,string> statusList = rc.GetClaimantStatusList(CentralDBConnString, companyId);
    rddlStatus.DataSource = statusList;
    rddlStatus.DataTextField = "Value";
    rddlStatus.DataValueField = "Key";
    rddlStatus.DataBind();
    rddlStatus.Items.Insert(0, new RadComboBoxItem("Select a status", string.Empty));
}

I had read in other threads about removing the DataBind() from the above code, but all that does is allow the load of the "Select A Status" item and none of the database items.

Thanks!

1 Answer, 1 is accepted

Sort by
0
Nencho
Telerik team
answered on 22 Oct 2012, 09:54 AM
Hello Jerry,

I could suggest you to bind the Text of the RadComboBox at code behind and use the binding expression to set the SelectedValue property in the markup. This approach is well described in our help article RadComboBox in RadGrid.


Kind regards,
Nencho
the Telerik team
 
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
 
Asked by
amonte
Top achievements
Rank 1
Answers by
Nencho
Telerik team
Share this question
or