Populate ComboBox in RadGrid at runtime

6 posts, 0 answers
  1. Sameers
    Sameers avatar
    63 posts
    Member since:
    Oct 2004

    Posted 09 Mar 2009 Link to this post

    I have a scenario where I need to populate a combobox in a cell based on the selection of another cell's combo box. That is, for example, there is a ShippingCarrier (UPS, USPS, FedEx etc) and when user select one of them, it should populate next cell's combo box with their services (for example, if UPS is selected, UPS Priority, UPS Next Day Air etc should be available in second column).

    I am using UnBound grid. My code seems to work, HOWEVER, when data is populated in second column, you can not drop down 2nd combo box using mouse (the only way to drop down is to put cursor on second drop down and use ALT+Down Arrow key). Below is the code I am using.

            col = AppendNewColumn(ColumnType.DropDown, 100, "Carrier""Carrier"
            Dim cCol As GridViewComboBoxColumn = CType(col, GridViewComboBoxColumn) 
            cCol.DataSource = GetShippingCarriers() 
            cCol.DisplayMember = "Display" 
            cCol.ValueMember = "Key" 

    AppendNewColumn is the function which creates column and add that to columns collection of Grid. next you can see that it get shipping carriers and bind to that.

    Now in CellEndEdit event, I am trying to populate second column as below.
     Private Sub grdMain_CellEndEdit(ByVal sender As ObjectByVal e As Telerik.WinControls.UI.GridViewCellEventArgs) Handles grdMain.CellEndEdit 
          If e.ColumnIndex = 12 Then 'Shipping Carrier Selected
                Dim row As GridViewRowInfo = Me.grdMain.Rows(e.RowIndex) 
                Dim newCarrier As String = row.Cells(e.ColumnIndex).Value.ToString 
                If newCarrier IsNot Nothing AndAlso newCarrier <> "" Then 
                    'Next column is Shipping Method (e.ColumnIndex + 1)
                    Dim cellElem As GridComboBoxCellElement = CType(row.Cells(e.ColumnIndex + 1).CellElement, GridComboBoxCellElement) 
                    Debug.Write(cellElem.ToString) 
     
                    Dim col As GridViewDataColumn = CType(cellElem.ColumnInfo, GridViewDataColumn) 
     
                    Dim element As New RadComboBoxElement 
                    ServiceInfo.UPSServices.ToArray() 
                    element.DataSource = GetShippingServices(CType(CInt(newCarrier), ShippingCarriers)) 
                    element.DisplayMember = "DisplayName" 
     
                    element.StretchHorizontally = True 
                    element.StretchVertically = True 
                    element.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDownList 
     
                    For Each obj As Object In cellElem.Children 
                        If TypeOf obj Is ComboBoxEditorLayoutPanel Then 
                            cellElem.Children.Remove(CType(obj, ComboBoxEditorLayoutPanel)) 
                        End If 
                    Next 
                    'cellElem.Children.Clear() 
                    cellElem.Children.Add(element) 
     
                    Debug.Write(col.FieldName) 
                End If 
            End If 

    Is there anything wrong with the above code that drop down is NOT selected correctly using mouse? 
    Also, as you can see that I am binding using name/value pair, however, from the cell object, I can get only Value, is there any way to get SelectedItem (Drop down item selected) which may further give me Text and Value? For example, row.Cells(e.ColumnIndex).Value.ToString returns value (numeric), however, display thing is Text (USPS, UPS etc). Is there any way to get either DisplayName or ValueMember value?

       Thanks,
       Sameers
  2. Martin Vasilev
    Admin
    Martin Vasilev avatar
    1061 posts

    Posted 12 Mar 2009 Link to this post

    Hi Sameers (theAngrycodeR),

    Thank you for writing.

    I have reviewed your code and in my opinion, changing the ComboBoxElement is not a good practice. Such approach could be difficult and can bring issues like described. Please, allow me to suggest a different way. You can use the ValueChanged event of the ComboBoxElement from your first column and implement custom filtering/populating code for the second ComboBox column depending on the selected value. You can subscribe to ValueChanged event by using RadGridView EditorRequired event. Also you can easily handle the chosen ValueMember and DisplayMember of the grid ActiveEditor property. Please, review the code-block below:

    Private Sub radGridView1_EditorRequired(ByVal sender As ObjectByVal e As Telerik.WinControls.UI.EditorRequiredEventArgs)   
        If e.EditorType Is GetType(RadComboBoxEditor) Then   
              
            AddHandler DirectCast(e.Editor, RadComboBoxEditor).ValueChanged, AddressOf Editor_ValueChanged   
              
        End If   
    End Sub   
     
    Private Sub Editor_ValueChanged(ByVal sender As ObjectByVal e As EventArgs)   
        If Me.radGridView1.CurrentCell.ColumnIndex = 3 Then   
            Dim selectedValue As Object = DirectCast(Me.radGridView1.ActiveEditor, RadComboBoxEditor).SelectedValue   
            Dim displayText As String = DirectCast(DirectCast(Me.radGridView1.ActiveEditor, RadComboBoxEditor).SelectedItem, RadComboBoxItem).Text   
              
            'implement your custom filtering/populating code here  
              
        End If   
          
    End Sub  

    I hope your trial period is going fine. Do not hesitate to contact me again if you have other questions.

    Best wishes,
    Martin Vasilev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. Rose
    Rose avatar
    1 posts
    Member since:
    Jun 2007

    Posted 21 Mar 2009 Link to this post

    I do have the same issues,can  anybody provide me with a sample source code so that it will be useful for me for further development


    Regards
    Seraphine Rosario
  4. Martin Vasilev
    Admin
    Martin Vasilev avatar
    1061 posts

    Posted 23 Mar 2009 Link to this post

    Hi Rose,

    Thank you for contacting us. Since, the implementation of such a scenario strongly depends of your needs I suggest you use the directions in my previous post. If you face any difficulties, please open a new support ticket and send me a small example project with detailed descriptions of your goal. I will be glad to help you to resolve the impediments. Do not hesitate to contact me again if you have any other questions.

    Best wishes,
    Martin Vasilev
    the Telerik team

    Check out Telerik Trainer , the state of the art learning tool for Telerik products.
  5. Aries
    Aries avatar
    2 posts
    Member since:
    Sep 2013

    Posted 04 Sep 2013 Link to this post

    Hi,

    I'm using telerik rad controls and I've encountered roadblocks.

    I've application that have RadGridView with multiple RadComboBox different datasource.

    Questions:

    How I'll bind a specific cell in RadGridView with ComboBox? Please see my codes below.

            private void grdTruck_CellBeginEdit(object sender, GridViewCellCancelEventArgs e)
            {
                DataTable dt = PopulateTruckList();

                GridViewComboBoxColumn comboCol = e.Column as GridViewComboBoxColumn;
                comboCol.DataSource = dt;
                
                ((GridViewComboBoxColumn)e.Column).DataSource = dt;
            }

    This code bind all the Rad Combo and I want only the specific cell..

    Thanks

  6. Nikolay
    Admin
    Nikolay avatar
    1805 posts

    Posted 06 Sep 2013 Link to this post

    Hello Aries,

    Please find the answer to your question in this forum thread.

    Regards,
    Nikolay
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
Back to Top