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

ComboBox within Grid issue...

3 Answers 151 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Richard
Top achievements
Rank 1
Richard asked on 15 Jul 2008, 09:22 AM
I am unable to set the selectedvalue of a combobox with a templatecolumn edititemtemplate of a grid.

The values of the combobox are set in the onitemcreated event. The combo box has the  SelectedValue='<%# Eval("insurer") %>' statement... however this prevents the grid from going into edit mode.

here is the code:

 <telerik:RadGrid
            ID="RadGrid1"
            runat="server"
            AutoGenerateColumns="False"
            AutoGenerateDeleteColumn="True"
            AllowSorting="True"
            AutoGenerateEditColumn="True"
            ondeletecommand="RadGrid1_DeleteCommand"
            oninsertcommand="RadGrid1_InsertCommand"
            onupdatecommand="RadGrid1_UpdateCommand"
            onneeddatasource="RadGrid1_NeedDataSource"
            GridLines="None"
            OnItemCreated="RadGrid1_ItemCreated"  >
            <MasterTableView
                CommandItemDisplay="Top"
                CommandItemSettings-AddNewRecordImageUrl="../Images/AddRecord.gif"
                CommandItemSettings-RefreshImageUrl="../Images/Refresh.gif"
                EditMode="InPlace"
                DataKeyNames="contractor_id,insurer,tstamp">
                <CommandItemSettings
                    AddNewRecordImageUrl="../Images/AddRecord.gif"
                    RefreshImageUrl="../Images/Refresh.gif">
                </CommandItemSettings>
                <RowIndicatorColumn>
                    <HeaderStyle Width="20px"></HeaderStyle>
                </RowIndicatorColumn>
                <ExpandCollapseColumn>
                    <HeaderStyle Width="20px"></HeaderStyle>
                </ExpandCollapseColumn>
                <Columns>
                   
                    <telerik:GridTemplateColumn UniqueName="TemplateColumn1">
                     <HeaderTemplate>Insurer</HeaderTemplate>
                     <ItemTemplate>
                         <%# Eval("insurer") %>
                     </ItemTemplate>
                     <EditItemTemplate>
                        <qese:QeseComboBox
                            ID="comboRadGrid1Insurer"
                            runat="server"
                            SelectedValue='<%# Eval("insurer") %>'>
                         </qese:QeseComboBox>
                     </EditItemTemplate>
                    </telerik:GridTemplateColumn>
                    <telerik:GridBoundColumn
                        DataField="employer_id"
                        HeaderText="Employer ID"
                        UniqueName="column1">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn
                        DataField="no_of_members"
                        HeaderText="No. of members"
                        UniqueName="column2">
                    </telerik:GridBoundColumn>
                </Columns>
            </MasterTableView>
            <HeaderStyle Font-Bold="False" Font-Italic="False" Font-Overline="False"
                Font-Size="XX-Small" Font-Strikeout="False" Font-Underline="False"
                Wrap="True" />
        </telerik:RadGrid>

    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridEditableItem)
        {
            QeseComboBox combo = (e.Item as GridEditableItem)["TemplateColumn1"].FindControl("comboRadGrid1Insurer") as QeseComboBox;
            if (combo != null)
            {
                combo.DataSource = Tbl_contractor_personal_accident_insurance.SelectDistinctInsurer();
                combo.DataTextField = "Value";
                combo.DataValueField = "Key";
                combo.DataBind();

            }
        }
    }

Any input would be appreciated?


3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 15 Jul 2008, 10:47 AM
Hi Richard,

Bind the SelectedValue property for the ComboBox as shown below.

ASPX:
<EditItemTemplate> 
    <qese:QeseComboBox 
         ID="comboRadGrid1Insurer" 
         runat="server" 
         SelectedValue='<%# Bind("insurer")%>' > 
    </qese:QeseComboBox> 
   </EditItemTemplate> 

You can also refer the following help article.
Known reasons for error messages

Thanks
Shinu.
0
Richard
Top achievements
Rank 1
answered on 16 Jul 2008, 01:06 AM
Thanks Shinu,

That works, however only if you do not bind the datasource in the itemcreated event, conversely it must done so in the objectdatasource.

Otherwise you get an error when it tries to set
SelectedValue='<%# Bind("insurer")%>'...

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

Looks like a timing issue, ie the # Bind is called before there is any data bound to the control.
0
Yavor
Telerik team
answered on 16 Jul 2008, 07:31 AM
Hi Richard,

The same behavior will hold true for a standard dropdown control as well. If you set the selected value for the control, do not bind it dynamically, and alternatively, if you bind it dynamically, in the code-behind, do not set the selected value. This will ensure the consistent behavior of the control.

Regards,
Yavor
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
Tags
Grid
Asked by
Richard
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Richard
Top achievements
Rank 1
Yavor
Telerik team
Share this question
or