Cascading comboboxes in RADGRID edit form

36 posts, 1 answers
  1. Reda
    Reda avatar
    19 posts
    Member since:
    Aug 2008

    Posted 01 Oct 2008 Link to this post

    Hello everyone,

    Can some one please help in solving this issue, I have a radgrid and using automatic updating/deleting/inserting and also using "edit form" provided by the grid. I have 2 comboboxes in my editform that i would like them to be related to each other. One combobox is the country the other is "state/province" depending if ur in Canada or the US. So if u select the country then the other combobox gets filled with appropriate values. I have looked every where and can't find a solution.

    Thanks in advance for any help.

    Reda.
  2. Dimo
    Admin
    Dimo avatar
    8330 posts

    Posted 01 Oct 2008 Link to this post

    Hi Reda,

    Here is an example of inter-related RadComboBoxes:

    http://demos.telerik.com/aspnet/prometheus/ComboBox/Examples/Functionality/MultipleComboBoxes/DefaultCS.aspx


    All the best,
    Dimo
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Reda
    Reda avatar
    19 posts
    Member since:
    Aug 2008

    Posted 01 Oct 2008 Link to this post

    Thanks for the reply,

    The thing is I know how to connect 2 combo boxes but having difficulties inside the edit form of the RADGRID. Any suggestions would be greatly appreciated.

    Thanks

    Reda
  5. Dio
    Dio avatar
    3 posts
    Member since:
    Oct 2007

    Posted 01 Oct 2008 Link to this post

    I have the same problem

    Telerik Team

    The problem is perform that in EDIT MODE

    using ClientSide Scripts

    Thanks




  6. Reda
    Reda avatar
    19 posts
    Member since:
    Aug 2008

    Posted 01 Oct 2008 Link to this post

    There must be some other way to perform this without using client side scripts?
  7. Dio
    Dio avatar
    3 posts
    Member since:
    Oct 2007

    Posted 02 Oct 2008 Link to this post

    I belive so

    im looking in this thread
    http://www.telerik.com/community/forums/thread/b311D-bgdcbm.aspx


    its the same problem

    i trying to use their solution

    if i maded it
     I try to do a simple sample e post it here!
  8. Reda
    Reda avatar
    19 posts
    Member since:
    Aug 2008

    Posted 07 Oct 2008 Link to this post

    Please Telerik team help me out with this problem....
  9. Yavor
    Admin
    Yavor avatar
    11 posts

    Posted 08 Oct 2008 Link to this post

    Hi Reda,

    You can correlate the combos as shown in the code sample below:

    .aspx
      <telerik:GridTemplateColumn UniqueName="TemplateColumn">  
                        <ItemTemplate> 
                        test  
                        </ItemTemplate> 
                        <EditItemTemplate> 
                        <telerik:RadComboBox runat="server" ID="combo1" AutoPostBack="true" 
                         DataSourceID="AccessDataSource2" DataTextField="EmployeeID"   
                        DataValueField="EmployeeID" OnSelectedIndexChanged="combo1_SelectedIndexChanged">  
                        </telerik:RadComboBox> 
                        <telerik:RadComboBox runat="server" ID="combo2" 
                        DataSourceID="AccessDataSource2" DataTextField="EmployeeID"   
                        DataValueField="EmployeeID">  
                        </telerik:RadComboBox> 
                        </EditItemTemplate> 
                          
                        </telerik:GridTemplateColumn> 

    .cs
    protected void combo1_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)  
        {  
            ((o as RadComboBox).NamingContainer.FindControl("combo2") as RadComboBox).SelectedIndex=(o as RadComboBox).SelectedIndex;  
        } 

    I hope this helps.

    Sincerely yours,
    Yavor
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  10. Reda
    Reda avatar
    19 posts
    Member since:
    Aug 2008

    Posted 14 Oct 2008 Link to this post

    That did not help because my drop down boxes are in an automatically created "edit form" by the grid. Any other solutions will be greatly appreciated?

    Thanks in advance.... 
  11. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 14 Oct 2008 Link to this post

    Hello Reda,

    I suppose that you would be using DropDownColumns in your grid. If so, you can access the dropdownlist in the editform for a column and then create its SelectedIndexChangedEventHandler  as shown in the code below.
    cs:
    protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e) 
            { 
                if (e.Item is GridEditableItem && e.Item.IsInEditMode) 
                { 
                     
                    DropDownList ddlist = (e.Item as GridEditableItem)["Country"].Controls[0] as DropDownList; 
                    
                    ddlist.AutoPostBack = true
                    ddlist.SelectedIndexChanged += new EventHandler(ddlist_SelectedIndexChanged); 
                } 
            } 


    To access the second dropdown in the EditForm on the SelectedIndexChanged event of the first dropdown you need to reference the edited grid item through the NamingContainer attribute of the first dropdown and then access the controls collection of the edited item as shown below.
    cs:
     void ddlist_SelectedIndexChanged(object sender, EventArgs e) 
        { 
              
                GridEditableItem editedItem = (sender as DropDownList).NamingContainer as GridEditableItem;          
               DropDownList ddList2 = editedItem["State"].Controls[0] as DropDownList; 
     
                // change the data source for ddList2 with custom code here       
               
        } 

    A similar scenario is also demonstrated in this online demo link.

    Thanks
    Princy.
  12. Reda
    Reda avatar
    19 posts
    Member since:
    Aug 2008

    Posted 15 Oct 2008 Link to this post

    Thank you for ur reply, I had allready checked out that demo and fixed the problem only in the edit mode. If i want to add new record not working.

    Any Help?

    Thanks in advance.........
  13. Reda
    Reda avatar
    19 posts
    Member since:
    Aug 2008

    Posted 16 Oct 2008 Link to this post

    Can someone plz help me?

    The suggestion above works only in edit command of the grid, if i want to add new record the cascading drop down list not populating based on what is chosen!!!

    Thanks in advance....
  14. Yavor
    Admin
    Yavor avatar
    11 posts

    Posted 17 Oct 2008 Link to this post

    Hi Reda,

    This is because the combo handler is not attached when inserting a new record. The code attached only checks for (e.Item is GridEditableItem && e.Item.IsInEditMode) which would correcspond to an item in edit mode.
    You can also add another check to veryfy if e.Item is GridEditFormInsertItem && e.Item.OwnerTableView.IsItemInserted to ensure you cater for the case when you are inserting an item.

    Kind regards,
    Yavor
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  15. Reda
    Reda avatar
    19 posts
    Member since:
    Aug 2008

    Posted 17 Oct 2008 Link to this post

    Do I have to put that check in the "ItemCreated" event handler of the grid?

     

  16. Yavor
    Admin
    Yavor avatar
    11 posts

    Posted 20 Oct 2008 Link to this post

    Hi Reda,

    This can also be done in the ItemDataBound event handler of the control.

    Kind regards,
    Yavor
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  17. Reda
    Reda avatar
    19 posts
    Member since:
    Aug 2008

    Posted 21 Oct 2008 Link to this post

    Thanks for the reply....

    I tried putting it into the "ItemDataBound" and its not working, it does an autopostback but the second drop down not getting filled properly.

    Here is the code:

    Dim

     

    currentTable As GridTableView = e.Item.OwnerTableView
    Dim tableName As String = e.Item.OwnerTableView.Name

     

     

    'Check to see if in insert mode
    If (TypeOf e.Item Is GridEditFormInsertItem AndAlso e.Item.OwnerTableView.IsItemInserted) Then

    '
    the item is in insert mode
    Dim editedItem As GridEditFormInsertItem = CType(e.Item, GridEditFormInsertItem)

     

     

    If tableName = "Master" Then
        'Country(Combobox)
        Dim ComboCountry As DropDownList = CType(editedItem("CountryID").Controls    (0),     DropDownList)

     

     

        'Attach SelectedIndexChanged event for the Country Combobox    control
        
    ComboCountry.AutoPostBack = True
        AddHandler ComboCountry.SelectedIndexChanged, AddressOf
        Me.ComboCountry_SelectedIndexChanged
    End If
    End If

    Not sure if i'm doing anything wrong?

    Thanks in advance,

    Reda

     

  18. Michael
    Michael avatar
    8 posts
    Member since:
    Nov 2007

    Posted 22 Oct 2008 Link to this post

    Hi Reda,

    I believe that we are facing a similar issue. If you look at the sample code (in C#) posted on the following thread, you will see that cascading comboboxes work quite well in EDIT mode.

    http://www.telerik.com/community/forums/thread/b311D-bebtae.aspx

    Like you, we discovered that the sample code does not work when in INSERT mode. With much hope and anticipation, I tried out the suggestion from this thread. Sadly, attaching the event handler this way makes no difference.

    My tests (using C#) have shown the event handler will be attached and fire using either set of conditions (converted to VB for this post):

    If (TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode) Then

    OR

    If (TypeOf e.Item Is GridEditFormInsertItem AndAlso e.Item.OwnerTableView.IsItemInserted) Then


    When stepping through the code, I find the problem relates to databinding within the event handler, not when/how the event handler is attached. When DataBind is called on the combobox in the OnSelectedIndexChanged event handler AFTER the DataSource has been assigned to the correct values from the database, the DataSource reverts to the previous values. The cascade effect occurs, but it is one cycle behind the user selection and subsequent data access.

    For example, the page loads with United States selected as the Country and the State combobox listing the correct values for the United States. First, we select Canada as the new Country. The event handler fires, the correct data is retreived from the database, databind is called and yet the State combobox still shows data from the United States. Second, when we select Mexico as the new Country, the Canadian states/provinces finally show up in the State combobox.

    We have experimented with several "interpretations" of the sample code and have not found a solution to this issue yet.

    Thanks,

    Michael
  19. Yavor
    Admin
    Yavor avatar
    11 posts

    Posted 23 Oct 2008 Link to this post

    Hi Michael,

    Attached to this message, is a small application, which handles the desired setup. It also uses client side handling to set the selected index of the related combo.
    I hope this gets you started proper.

    Sincerely yours,
    Yavor
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  20. Michael
    Michael avatar
    8 posts
    Member since:
    Nov 2007

    Posted 23 Oct 2008 Link to this post

    Hi Yavor,

    Thank you for taking a look at this issue. Unfortunately, there is a small misunderstanding regarding what the issue actually is. Your sample code solves a problem other than the one we have. In your sample, javascript on the client selects the same index in the second drop-down column as the item selected in the first drop-down column.

    I (like Reda and perhaps others) am working on an address form where the correct list of states/provinces should be displayed based on the country selected. There is sample code (provided by Telerik) that works when editing an existing record using the built-in edit form, but it does not work when inserting a new record.

    In my tests of both the sample code and our own implementation, the event handler fires, the correct data is retreived from the database, and that data is assigned to the data source for the second drop-down. The failure occurs when DataBind is called on the second drop-down immediately after the data source is assigned to the new data.

    Here are the steps to reproduce the issue:

    1. Download the following code sample from Telerik: http://www.telerik.com/ClientsFiles/087586_CascadingCombosInGridEditMode.zip
    2. Enable inserting
    3. Run the sample
    4. When editing an existing record, change selection of "Order ID" drop-down and note that the values listed in the "Quantities in stock" drop-down change.
    5. When adding a new record, change selection of "Order ID" drop-down and note that the values listed in the "Quantities in stock" drop-down do not change as they do when editing a record. THIS is the issue. The second drop-down should be loaded with new data.

    Here is a link to the live demo of the sample code for anyone researching a similar issue:
    http://demos.telerik.com/ASPNET/Prometheus/Grid/Examples/Programming/AccessingCellsAndRows/DefaultCS.aspx

    Thanks,

    Michael

  21. Reda
    Reda avatar
    19 posts
    Member since:
    Aug 2008

    Posted 23 Oct 2008 Link to this post

    Well put Michael, this issue has been haunting me for the last few days...
  22. Yavor
    Admin
    Yavor avatar
    11 posts

    Posted 27 Oct 2008 Link to this post

    Hello Reda,

    Indeed, the behavior that you described is observed.
    I will review it in greater details, and update this thread as needed.

    All the best,
    Yavor
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  23. Answer
    Yavor
    Admin
    Yavor avatar
    11 posts

    Posted 27 Oct 2008 Link to this post

    Hello Reda,

    After further detailed review on the case, it seems that the comboBox is not properly databound in case it is nested in an insert item form. To work-around this behavior, you can iterate through the items returned in the datatable (datasource for the combo), adding each item to the combo.
    Adding an item to the combo may look like this:

    .cs
       void list_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)  
            {  
                //first reference the edited grid item through the NamingContainer attribute  
                GridEditableItem editedItem = (sender as RadComboBox).NamingContainer as GridEditableItem;  
     
                //the dropdown list will be the first control in the Controls collection of the corresponding cell  
                //for custom edit forms (WebUserControl/FormTemplate) you can find the column editor with the FindControl(controlId) method  
     
                RadComboBox ddList = editedItem["ddlQuantity"].Controls[0] as RadComboBox;              
                //GridDropDownListColumnEditor editor = editedItem.EditManager.GetColumnEditor("ddlQuantity") as GridDropDownListColumnEditor;  
     
                  
                // change the data source for ContactTitle with custom code here  
                DataTable table = GetRelatedRecords("SELECT OrderID, Quantity FROM [Order Details] WHERE OrderID = " + (editedItem["ddlOrderID"].Controls[0] as RadComboBox).SelectedValue);  
                           
                ddList.DataBind();  
                ddList.Items.Clear();  
                ddList.Items.Add(new RadComboBoxItem("test"));  
                //ddList.DataBind();  
               // ddList.Items.Add(new ListItem("test"));  
                  
                RadGrid1.Controls.Add(new LiteralControl("<b>The available options for 'Quantites in stock' has been changed</b>"));  
            } 

    I hope this helps.

    Regards,
    Yavor
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  24. Michael
    Michael avatar
    8 posts
    Member since:
    Nov 2007

    Posted 28 Oct 2008 Link to this post

    Hi Yavor,

    I have modified our implementation following your suggestion and the cascading effect does work for inserts as well as edits. Looping through the data and adding the items into the Combobox may not be especially elegant, but it is effective. Obviously, we were expecting this functionality to be handled properly by the Grid and/or Combobox controls.

    Has this DataBinding bug been added to the list of issues to be addressed in a future release? How soon can we expect to see the issue resolved?

    Thanks,

    Michael
  25. Yavor
    Admin
    Yavor avatar
    11 posts

    Posted 29 Oct 2008 Link to this post

    Hi Michael,

    I have escalated this issue to our developers, and although I cannot commit to a definite timeframe, this will be looked into for sure.
    I hope the present work-around is suitable for you.

    All the best,
    Yavor
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  26. Reda
    Reda avatar
    19 posts
    Member since:
    Aug 2008

    Posted 03 Nov 2008 Link to this post

    Hi Yavor,
     
    Sorry I haven't replied in a while but I have been travelling work related for 2 weeks now and finally now i'm at the office. I implemented your solution and it works perfect. It would still be greatly appreciated if the grid would handle that problem automatically. But besides that thanks for your help Yavor and Micheal I appreciated your feedbacks, thanks.

    Bye Reda
  27. Rajitha Reshmai
    Rajitha Reshmai avatar
    2 posts
    Member since:
    Jun 2009

    Posted 10 Feb 2010 Link to this post

    How to close the edit mode after  inserting a new record in radgrid
  28. Pavlina
    Admin
    Pavlina avatar
    6084 posts

    Posted 15 Feb 2010 Link to this post

    Hello Rajitha,

    Did you set AllowAutomaticInserts to true? If that is the case, please set it to false. This propertie is used only with datasource controls as shown here.

    Additionally, to close the edit mode after  inserting a new record in radgrid you could try the code snippet bellow:
    C#
    RadGrid1.MasterTableView.IsItemInserted = false;

    I hope this helps.

    Kind regards,
    Pavlina
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  29. Jaime Bula
    Jaime Bula avatar
    41 posts
    Member since:
    Apr 2009

    Posted 12 Aug 2010 Link to this post

    Hi,

    Is there an example on how to do this por RadGridView for Silverlight?

    Thanx in Advance.

    Jaime
  30. Dimo
    Admin
    Dimo avatar
    8330 posts

    Posted 13 Aug 2010 Link to this post

    Hello Jaime,

    Please refer to:

    http://blogs.telerik.com/pavelpavlov/posts/10-01-27/cascading_comboboxes_selection_in_radgridview_for_silverlight_and_wpf.aspx

    If you have additional questions, it is preferrable to post a new thread in the Silverlight forum section.

    Kind regards,
    Dimo
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  31. Balakrishna Reddy
    Balakrishna Reddy avatar
    37 posts
    Member since:
    Oct 2012

    Posted 05 Oct 2012 Link to this post

    hey
       i also have same problem.if i use like this i am getting an error as
    NullReferenceException was not handled by the user code  & Object Reference not set to an instance of an Object.
    like this....
    so please suggest me how to solve  this problem.


    Thanks in advance
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017