How to put a databound ComboBox in a column of a GridView?

4 posts, 0 answers
  1. Jill-Connie Lorentsen
    Jill-Connie Lorentsen avatar
    118 posts
    Member since:
    Nov 2008

    Posted 06 Oct 2011 Link to this post

    I have a GridView where one of the columns is a GridViewComboBoxColumn.

    After the grid has been populated by setting the DataSource to a list gotten from the database, I want to populate the ComboBox column.

    The ComboBox column contains indexes(integers), which I want to be ValueMembers.
    The ComboBox' DataSource is set to another list from the database, and the ValueMember and DisplayMember is set. But when setting the DisplayMember I get an exception saying "Object must be of type Int32". The DisplayMember I try to set is a string.

    Is there a way to put a databound ComboBox in the column, where the value member is from one list and the display member from another?


    Regards, Jill-Connie Lorentsen

  2. Martin Vasilev
    Admin
    Martin Vasilev avatar
    1061 posts

    Posted 11 Oct 2011 Link to this post

    Hi Jill-Connie Lorentsen,

    Thank you for writing.

    I am not completely sure if I understand your scenario very well, however setting the ValueMember and DisplayMember from different data sources is not a supported scenario. For a detailed description and sample code on how to use combo-box column in RadGridView, please take a look at our product documentation.

    Let me know if you still have any additional questions.

    Kind regards,
    Martin Vasilev
    the Telerik team

    Q2’11 SP1 of RadControls for WinForms is available for download (see what's new); also available is the Q3'11 Roadmap for Telerik Windows Forms controls.

  3. Jill-Connie Lorentsen
    Jill-Connie Lorentsen avatar
    118 posts
    Member since:
    Nov 2008

    Posted 11 Oct 2011 Link to this post

    Well, it seems like you have a solution to my scenario, even though I don't quite get it... Magic?

    I make sure that the names of the columns in my GridView are the same as the name of the ValueMember of each ComboBox dataset, and then the correct value is displayed. I can't find anything about this in the documentation, but it works.

    Here is how I've done it, in case someone else has the same problem.

    The grid is populated from this stored procedure in the database:
    SELECT 
            r.RequestID,    
            r.DepartementID,
            r.OfficerID,    
            r.Comment 
        from Request r      
            inner join Departement d on r.DepartementID = d.DepartementID
            inner join Employee e on r.OfficerID = e.EmployeeID             
        where r.Date > ISNULL(@fromDate, '17700101')
        and   r.Date < ISNULL(@toDate, '20991231')



    This is how the columns are added:
    GridViewComboBoxColumn officerColumn = new GridViewComboBoxColumn();
    getOfficerNames(ref officerColumn);
    officerColumn.DropDownStyle = RadDropDownStyle.DropDown;
    radGridRequests.MasterTemplate.Columns.Add(officerColumn);


    And finally how the column combo box is populated:

    private void getOfficerNames(ref GridViewComboBoxColumn column)
            {
                WcfCommonProxy proxy = null;
                object columnValues = typeof(List<>);
      
                try
                {
                    proxy = new WcfCommonProxy();
      
                    columnValues = proxy.GetOfficer();
      
                    if (columnValues != null)
                    {
                        column.DataSource = columnValues;
                        column.FieldName = "OfficerID";
                        column.DisplayMember = "OfficerName";
                        column.ValueMember = "OfficerID";
                    }
                    proxy.Close();
                    proxy = null;
                }
                catch (Exception ex)
                {
                    if (proxy != null) proxy.Abort();
                    //error handling
                }        
            }

    The proxy.GetOfficer() is also a call to a stored procedure in the database:

    SELECT  EmployeeID as OfficerID, 
        FirstName +' '+ LastName as OfficerName
    FROM    Employee
  4. Martin Vasilev
    Admin
    Martin Vasilev avatar
    1061 posts

    Posted 14 Oct 2011 Link to this post

    Hello Jill-Connie Lorentsen,

    Thank you for getting back to me. Glad you have found a solution for your scenario. Do not hesitate to contact me again if you have any other questions.

    Kind regards,
    Martin Vasilev
    the Telerik team

    Q2’11 SP1 of RadControls for WinForms is available for download (see what's new); also available is the Q3'11 Roadmap for Telerik Windows Forms controls.

Back to Top