Combox : How Assign Value to Predefined Combox Column

6 posts, 1 answers
  1. LEON
    LEON avatar
    13 posts
    Member since:
    Nov 2018

    Posted 07 Nov 2018 Link to this post

    Hi everyone

    While I am learning Telerik Radgridview control, I am stuck with combo box column example. I have created the grid and columns using GUI property builder. It's not bound to any database. I just want to display a simple drop-down list. For example:- {Yes, No, Maybe}

    My Question
    1. How to assign the drop-down list Item? {Yes, No, Maybe}
    2. Is it possible to assign different values to each combo box cell per row
        For Example:-

                              row(0)= {Yes, No, Maybe},

                              row(1)={Absoluitely, Definately, Certain}
    Technical Info
    Gridview Name: radGridView1
    Combobox Column Name: status
    Application Type: VB.net WinForm

     

    I think its a very basic question, but somehow I am unable to find the solution. It will be very kind if someone guide me to the solution.

     

  2. LEON
    LEON avatar
    13 posts
    Member since:
    Nov 2018

    Posted 07 Nov 2018 in reply to LEON Link to this post

    I have found the solution for my 1st question

    1. How to assign the drop-down list Item? {Yes, No, Maybe}

      Code Snippest

    Dim comboItems() As String = {"Yes", "No", "Maybe"}
    CType(radGridView1.Columns("status"), GridViewComboBoxColumn).DataSource = comboItems

     

    But have not found the solution for my 2nd question

  3. Answer
    Dess | Tech Support Engineer, Sr.
    Admin
    Dess | Tech Support Engineer, Sr.  avatar
    3758 posts

    Posted 08 Nov 2018 Link to this post

    Hello, LEON, 

    Indeed, the GridViewComboBoxColumn is the appropriate way to display drop down values when editing a cell. 

    As to the second question, note that it is very important to set the GridViewComboBoxColumn.DataSource property to a collection that contains all possible values in the columns. Then, if you want just a specific subset of options to be displayed for the different rows, it is necessary to handle the CellEditorInitialized event and specify the DataSource collection for the editor. Note that this collection must be a subset of the already applied DataSource for the column. You can find below a sample code snippet which result is illustrated in the attached gif file:
    public RadForm1()
    {
        InitializeComponent();
     
        GridViewComboBoxColumn comboColumn = new GridViewComboBoxColumn("Combo column");
        comboColumn.DataSource = GetDataSourceValues(true, -1);
        comboColumn.Width = 200;
        this.radGridView1.Columns.Add(comboColumn);
        this.radGridView1.Rows.Add("No");
        this.radGridView1.Rows.Add("High");
        this.radGridView1.CellEditorInitialized += radGridView1_CellEditorInitialized;
    }
     
    private void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
    {
        RadDropDownListEditor editor = e.ActiveEditor as RadDropDownListEditor;
        if (editor != null)
        {
            RadDropDownListEditorElement el = editor.EditorElement as RadDropDownListEditorElement;
            BindingList<string> source = GetDataSourceValues(false, e.RowIndex);
            el.DataSource = source;
            el.SelectedIndex = source.IndexOf(e.Row.Cells[e.ColumnIndex].Value + "");
        }
    }
     
    private BindingList<string> GetDataSourceValues(bool allValues, int rowIndex)
    {
        BindingList<string> options = new BindingList<string>();
        if (allValues || rowIndex % 2 == 0)
        {
            options.Add("Yes");
            options.Add("No");
            options.Add("Maybe");
        }
        if (allValues || rowIndex % 2 == 1)
        {
            options.Add("Low");
            options.Add("Medium");
            options.Add("High");
        }
        return options;
    }


    I hope this information helps.

    Regards,
    Dess | Tech Support Engineer, Sr.
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
  4. LEON
    LEON avatar
    13 posts
    Member since:
    Nov 2018

    Posted 10 Nov 2018 Link to this post

    @Dess Sorry for the late reply, I was out of town for some work.
    Your code snippet is easy to understand. I have only one issue.
    I believe your code is in C#, I am developing using VB.net. I already converted the code using Telerik code converter. Below is the converted code..

    Its giving me error on line no 16

    Error Description (Line No 16)

    'Public Event CellEditorInitialized As GridViewCellEventHandler' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.

    01.Imports System.ComponentModel
    02.Imports System.Data.OleDb
    03.Imports System.IO
    04.Imports Telerik.WinControls.UI
    05.
    06.Public Class RadForm1
    07.
    08.    Public Sub New()
    09.        InitializeComponent()
    10.        Dim comboColumn As GridViewComboBoxColumn = New GridViewComboBoxColumn("Combo column")
    11.        comboColumn.DataSource = GetDataSourceValues(True, -1)
    12.        comboColumn.Width = 200
    13.        Me.RadGridView1.Columns.Add(comboColumn)
    14.        Me.RadGridView1.Rows.Add("No")
    15.        Me.RadGridView1.Rows.Add("High")
    16.        Me.RadGridView1.CellEditorInitialized += AddressOf radGridView1_CellEditorInitialized
    17.    End Sub
    18.
    19.    Private Sub radGridView1_CellEditorInitialized(ByVal sender As Object, ByVal e As GridViewCellEventArgs)
    20.        Dim editor As RadDropDownListEditor = TryCast(e.ActiveEditor, RadDropDownListEditor)
    21.
    22.        If editor IsNot Nothing Then
    23.            Dim el As RadDropDownListEditorElement = TryCast(editor.EditorElement, RadDropDownListEditorElement)
    24.            Dim source As BindingList(Of String) = GetDataSourceValues(False, e.RowIndex)
    25.            el.DataSource = source
    26.            el.SelectedIndex = source.IndexOf(e.Row.Cells(e.ColumnIndex).Value & "")
    27.        End If
    28.    End Sub
    29.
    30.    Private Function GetDataSourceValues(ByVal allValues As Boolean, ByVal rowIndex As Integer) As BindingList(Of String)
    31.        Dim options As BindingList(Of String) = New BindingList(Of String)()
    32.
    33.        If allValues OrElse rowIndex Mod 2 = 0 Then
    34.            options.Add("Yes")
    35.            options.Add("No")
    36.            options.Add("Maybe")
    37.        End If
    38.
    39.        If allValues OrElse rowIndex Mod 2 = 1 Then
    40.            options.Add("Low")
    41.            options.Add("Medium")
    42.            options.Add("High")
    43.        End If
    44.
    45.        Return options
    46.    End Function
    47.
    48.End Class
  5. LEON
    LEON avatar
    13 posts
    Member since:
    Nov 2018

    Posted 10 Nov 2018 Link to this post

    sorry about the double post, I cant find the post edit button.

    above issue is resolebed. And my question is well answered. Thanks to Dess. For future refference below is the change I have made on line no 16.

    AddHandler Me.RadGridView1.CellEditorInitialized, AddressOf radGridView1_CellEditorInitialized
  6. Dess | Tech Support Engineer, Sr.
    Admin
    Dess | Tech Support Engineer, Sr.  avatar
    3758 posts

    Posted 13 Nov 2018 Link to this post

    Hello, LEON,  

    I am glad that the problem you were facing is now resolved. Feel free to use our online converter from C# to VB and vice versa: http://converter.telerik.com/

    It is quite useful when converting a considerable amount of code.

    I hope this information helps. 

    Regards,
    Dess | Tech Support Engineer, Sr.
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top