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

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

3 Answers 107 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jill-Connie Lorentsen
Top achievements
Rank 1
Jill-Connie Lorentsen asked on 06 Oct 2011, 12:14 PM
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

3 Answers, 1 is accepted

Sort by
0
Martin Vasilev
Telerik team
answered on 11 Oct 2011, 07:57 AM
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.

0
Jill-Connie Lorentsen
Top achievements
Rank 1
answered on 11 Oct 2011, 08:40 AM
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
0
Martin Vasilev
Telerik team
answered on 14 Oct 2011, 09:25 AM
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.

Tags
GridView
Asked by
Jill-Connie Lorentsen
Top achievements
Rank 1
Answers by
Martin Vasilev
Telerik team
Jill-Connie Lorentsen
Top achievements
Rank 1
Share this question
or