Set different ItemSource for comboboxcolumn in grid

Nov 28, 2008 at 11:28 AM
Edited Nov 28, 2008 at 11:54 AM
Hi,

I am using DataGrid released with latest WPF Toolkit.

I have a requirement to bind datagrid column of type DataGridComboBoxColumn to different ItemSource in each row of a grid.

How can it be achieved?

Eg:

In Row1 of grid, Column is binded to ItemSource itmSrc1

In Row2 of grid, Column is binded to ItemSource itmSrc2 and so on.

Code:

DataGridComboBoxColumn _colResponse;

         System.Collections.ObjectModel.Collection<ResponseAndRating> resRating ;

         System.Collections.ObjectModel.Collection<ResponseAndRating> resRating1;

 

DataGrid dg = new DataGrid();

            dg.AutoGenerateColumns = false;

 

 

            DataGridTextColumn colFirstName = new DataGridTextColumn();

            colFirstName.Header = "Rating Question";

            colFirstName.Binding = new Binding("FirstName");

           // colFirstName.Width = 100;

            dg.Columns.Add(colFirstName);

 

 

            DataGridTextColumn colLastName = new DataGridTextColumn();

            colLastName.Header = "Remarks";

            colLastName.Binding = new Binding("LastName");

            // colLastName.Width = 100;

            dg.Columns.Add(colLastName);

 

            DataGridTextColumn colEmployeeNo = new DataGridTextColumn();

            colEmployeeNo.Header = "InfoSource";

            colEmployeeNo.Binding = new Binding("EmployeeNumber");

            // colEmployeeNo.Width = 100;

            dg.Columns.Add(colEmployeeNo);

 

             resRating = new System.Collections.ObjectModel.Collection<ResponseAndRating>();

             resRating1 = new System.Collections.ObjectModel.Collection<ResponseAndRating>();

 

            _colResponse = new DataGridComboBoxColumn();

            _colResponse.Header = "ResponseAndRating";

            _colResponse.ItemsSource = resRating; [ItemSource is required to be changed per row]

            _colResponse.SelectedValueBinding = new Binding("ResponseAndRating");

            _colResponse.DisplayMemberPath = "ResponseDesc";

            _colResponse.SelectedValuePath = "ResponseId";

            //colResponse.DisplayMemberPath = ("ResponseAndRating");

            //colResponse.Width = 100;

            dg.Columns.Add(_colResponse);

 

 

          

            System.Collections.ObjectModel.Collection<EmployeeNew> emp = new System.Collections.ObjectModel.Collection<EmployeeNew>();

 

 

            ResponseAndRating resRtg = new ResponseAndRating();

            resRtg.ResponseId = 1;

            resRtg.ResponseDesc = "A";

 

            resRating.Add(resRtg);

 

            ResponseAndRating resRtg1 = new ResponseAndRating();

            resRtg1.ResponseId = 1;

            resRtg1.ResponseDesc = "B";

 

            resRating1.Add(resRtg1);

 

            ResponseAndRating resRtg2 = new ResponseAndRating();

            resRtg2.ResponseId = 1;

            resRtg2.ResponseDesc = "C";

 

            resRating1.Add(resRtg2);

 

            EmployeeNew employee = new EmployeeNew();

            employee.FirstName = "Abc";

            employee.LastName = "Dv";

            employee.EmployeeNumber = "1";

            employee.ResponseAndRating = resRating;

 

            emp.Add(employee);

 

            EmployeeNew employee1 = new EmployeeNew();

 

            employee1.FirstName = "Xyz";

            employee1.LastName = "Dv";

            employee1.EmployeeNumber = "2";

            employee1.ResponseAndRating = resRating1;

 

            emp.Add(employee1);

 

 

            dg.ItemsSource = emp;

 

            // //Binding b = new Binding();

           // //b.Source = arr1;

 

           // dataGrid1.Name = "Employee Information";

 

           // DataGridColumn gvc1 = new DataGridColumn();

           ////DataGridTe gvc1 = new GridViewColumn();

           // gvc1. = new Binding("FirstName");

           // gvc1.Header = "FirstName";

           // gvc1.Width = 100;

           // myGridView.Columns.Add(gvc1);

           // GridViewColumn gvc2 = new GridViewColumn();

           // gvc2.DisplayMemberBinding = new Binding("LastName");

           // gvc2.Header = "Last Name";

           // gvc2.Width = 100;

           // myGridView.Columns.Add(gvc2);

           // GridViewColumn gvc3 = new GridViewColumn();

           // gvc3.DisplayMemberBinding = new Binding("EmployeeNumber");

           // gvc3.Header = "Employee No.";

           // gvc3.Width = 100;

           // myGridView.Columns.Add(gvc3);

 

           // GridViewColumn gvc4 = new GridViewColumn();

           // gvc4.Header = "RatingAndResponse";

           // myGridView.Columns.Add(gvc4);

 

          

 

           // System.Collections.ObjectModel.Collection<Employee> emp = new System.Collections.ObjectModel.Collection<Employee>();

           // Employee employee = new Employee();

           // employee.FirstName = "Hiral";

           // employee.LastName = "Dave";

           // employee.EmployeeNumber = "1";

           // employee.ResponseAndRating = arr1;

 

           // emp.Add(employee);

 

           // Employee employee1 = new Employee();

 

           // employee1.FirstName = "Bg";

           // employee1.LastName = "Dave";

           // employee1.EmployeeNumber = "2";

           // employee1.ResponseAndRating = arr2;

 

           // emp.Add(employee1);

 

          

          

        }

 

    public class EmployeeNew

    {

 

        private string _firstName;

        private string _lastName;

        private string _empNumber;

        private System.Collections.ObjectModel.Collection<ResponseAndRating> _responseAndRating;

 

 

 

        public string FirstName

        { get { return _firstName; } set { _firstName = value; } }

 

        public string LastName

        { get { return _lastName; } set { _lastName = value; } }

 

        public string EmployeeNumber

        { get { return _empNumber; } set { _empNumber = value; } }

 

        public System.Collections.ObjectModel.Collection<ResponseAndRating> ResponseAndRating

        {

            get { return _responseAndRating; }

            set { _responseAndRating = value; }

        }

 

    }

 

public class ResponseAndRating

{

    private int _responseId;

    private string _responseDesc;

 

    public int ResponseId

    {

        get { return _responseId; } set { _responseId = value; }

    }

    public string ResponseDesc

    {get { return _responseDesc; } set { _responseDesc = value; }

    }

}

 

 


Regards,
HV
Coordinator
Dec 18, 2008 at 6:46 PM
I'm not sure if this addresses your scenario exactly, but it may help.  The sample is here, http://blogs.msdn.com/vinsibal/archive/2008/12/17/wpf-datagrid-dynamically-updating-datagridcomboboxcolumn.aspx.