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

GridTemplateColumn Cascading RadComboBox Binding

4 Answers 159 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Marcus
Top achievements
Rank 1
Marcus asked on 04 Jul 2011, 03:25 PM
Hello Everyone...

I have a problem Binding ComboBoxes. Here is my example:-
I have a table in my database which has a list of company vehicles. Another table has a complete list of any vehicles ID, vehicleMake, vehicleModel, vehicleEngineSize.

In edit mode the company vehicles can be changed colour etc. Also there are three comboboxes. 1: List all the possible Makes. Ford, Volvo etc. The second all Models and the final one all engine sizes.

So that the user is only able to select the correct combination the boxes load by EnableLoadOnDemand. I used the Telerik help file here: Help File

Ok so my problem comes when I want to update any possible changes.

THIS WORKES:-
<
telerik:GridTemplateColumn  DataField="vehicleModel" HeaderText="vehicleModel"
     SortExpression="vehicleModel" UniqueName="vehicleModel">
     <EditItemTemplate>
         <telerik:RadComboBox ID="rcb_vehicleModel" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"vehicleModel")%>' EnableLoadOnDemand="True" OnItemsRequested="rcb_vehicleModel_ItemsRequested" AutoPostBack="true">
         </telerik:RadComboBox>                          
 
     </EditItemTemplate>
     <ItemTemplate>
         <asp:Label ID="lbl_vehicleModel" runat="server" Text='<%# Eval("vehicleModel") %>'></asp:Label>
     </ItemTemplate>
     <HeaderStyle Width="6em" />
 </telerik:GridTemplateColumn>


DOES NOT WORK:-
<
telerik:GridTemplateColumn DataField="vehicleModel"  HeaderText="vehicleModel"
    SortExpression="vehicleModel" UniqueName="vehicleModel">
    <EditItemTemplate>
        <telerik:RadComboBox ID="rcb_vehicleModel" runat="server" DataSourceID="sqlds_vehicleModel" DataTextField="vehicleModel"
            DataValueField="vehicleMake" SelectedValue="<%# bind('vehicleModel') %>" AutoPostBack="True">
        </telerik:RadComboBox>
    </EditItemTemplate>
    <ItemTemplate>
        <asp:Label ID="lbl_vehicleModel" runat="server" Text='<%# Eval("vehicleModel") %>'></asp:Label>
    </ItemTemplate>
    <HeaderStyle Width="6em" />
</telerik:GridTemplateColumn>

As you can see in the first example there is no SelectedValue="<%# bind('vehicleModel') %>" in the above template. If I include this i get the error:-

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

But without the Bind the changes obviously are not returned to the table

Protected Sub rcb_vehicleModel_ItemsRequested(ByVal sender As Object, ByVal e As RadComboBoxItemsRequestedEventArgs)
 
     Dim editedItem As GridEditableItem = CType(CType(sender, RadComboBox).NamingContainer, GridEditableItem)
 
     Dim ddl_make As RadComboBox = DirectCast(editedItem.FindControl("rcb_vehicleMake"), RadComboBox)
     Dim ddl_model As RadComboBox = DirectCast(editedItem.FindControl("rcb_vehicleModel"), RadComboBox)
     Dim ddl_vehicleEngine As RadComboBox = DirectCast(editedItem.FindControl("rcb_vehicleEngineSize"), RadComboBox)
 
 
     Dim SQLstr As String
     SQLstr = "SELECT DISTINCT vehicleModel FROM dbo.vehicleTypes WHERE (vehicleMake = '" & ddl_make.SelectedValue.ToString & "')"
 
     ' change the data source for ContactTitle with custom code here
 
     sqlds_vehicleModel.SelectCommand = SQLstr
 
     ddl_model.DataValueField = "vehicleModel"
     ddl_model.DataTextField = "vehicleModel"
 
     ddl_model.DataSource = sqlds_vehicleModel
     ddl_model.DataBind() '********@ERROR HERE AS ABOVE: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control
 
 End Sub

My question therefore is how do I rebind the changes back to the database. I think the error above is because I am calling the Bind to early but I'm not sure... Hopefully someone will have the answer as this problem is driving me mad!!!

Thanx for looking. Marcus

PS Can any code suggestions be in VB pls

4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 05 Jul 2011, 05:28 AM
Hello Marcus,

Try binding the Text property of the combo box instead. The SelectedValue might not be initialized in this scenario:
aspx:
 <EditItemTemplate>
       <telerik:RadComboBox ID="rcb_vehicleModel" runat="server" DataSourceID="sqlds_vehicleModel" DataTextField="vehicleModel" DataValueField="vehicleMake" Text='<%# Bind("vehicleModel") %>' AutoPostBack="True">
       </telerik:RadComboBox>
</EditItemTemplate>

Thanks,
Shinu.
0
Marcus
Top achievements
Rank 1
answered on 05 Jul 2011, 10:34 AM
Hi Shinu,

Thanx for your suggestion. But when you click on the second dropdown ie the car model. It runs to the codebehind and processes the following:-

Dim editedItem As GridEditableItem = CType(CType(sender, RadComboBox).NamingContainer, GridEditableItem)
 
Dim ddl_make As RadComboBox = DirectCast(editedItem.FindControl("rcb_vehicleMake"), RadComboBox)
Dim ddl_model As RadComboBox = DirectCast(editedItem.FindControl("rcb_vehicleModel"), RadComboBox)
Dim ddl_vehicleEngine As RadComboBox = DirectCast(editedItem.FindControl("rcb_vehicleEngineSize"), RadComboBox)
 
 
Dim SQLstr As String
SQLstr = "SELECT DISTINCT vehicleModel FROM dbo.vehicleTypes WHERE (vehicleMake = '" & ddl_make.SelectedValue.ToString & "')"
 
' change the data source for ContactTitle with custom code here
 
sqlds_vehicleModel.SelectCommand = SQLstr
 
ddl_model.DataSource = sqlds_vehicleModel
 
ddl_model.DataBind()

after the DataBind

<telerik:RadComboBox ID="rcb_vehicleModel" runat="server" Text='<%# bind("vehicleModel") %>' EnableLoadOnDemand="True" OnItemsRequested="rcb_vehicleModel_ItemsRequested" AutoPostBack="true">
                            </telerik:RadComboBox>

 I get the following error:-

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

I have see an article which describes the issue but it is in C# and therefore it make little sense to me - maybe you can take a look or offer another solution.

Article describing the issue

Thanx. Marcus

0
Princy
Top achievements
Rank 2
answered on 05 Jul 2011, 10:44 AM
Hello Marcus,

I hope the following help article might help you to solve the issue.
RadComboBox in RadGrid.

Thanks,
Princy.
0
Marcus
Top achievements
Rank 1
answered on 05 Jul 2011, 03:15 PM
Hi Princy

I have tried the project as you suggest and it seems the the two are not the same. I have created a simple VS project with its own db to explain the issue. I see I'm not able to upload it here but if you have time to look it can be downloaded from here:-

Sample Project

Thanx for your time,

Marcus
Tags
Grid
Asked by
Marcus
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Marcus
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Share this question
or