0

This is my object structure

       class object 
       {
           string projectname;
           string projectid;
           list<string> associated_students;
       }

//The List I am binding to the grid

       list<objects> objectList = getList();
       dataGridView.Source =objectList;

Now I want to bind the combo box inside the datagrid with the list "associated_students"

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Souradeep Das
  • 17
  • 1
  • 1

1 Answers1

0

If I understand the question, you want each row to be tied to an object within your list of objects and you want the third column to show a combobox of that object's unique list of associated students. If I am correct, a simple search leads to this similar question:

How do I set up a DataGridView ComboBoxColumn with a different DataSource in each cell?

To solve, you need to manually bind each row. I was able to duplicate your problem and came up with this solution:

Your class "object"

public class Assignment
{
  public Assignment()
  {
    this.Associated_Students = new List<string>();
  }

  public string ProjectName { get; set; }
  public string ProjectID { get; set; }
  public List<string> Associated_Students { get; set; }
}

And in Form1:

public Form1()
{
  InitializeComponent();

  this.Assignments = new List<Assignment>()
  {
    new Assignment()
    {
      ProjectID = "1",
      ProjectName = "First",
      Associated_Students = new List<string>() { "Me", "You", "Him", "Her" }
    },

    new Assignment()
    {
      ProjectID = "2",
      ProjectName = "Second",
      Associated_Students = new List<string>() { "Foo", "Bar" }
    }
  };

  this.BindDGViewToList();
}

public List<Assignment> Assignments { get; set; }

public void BindDGViewToList()
{
  DataGridViewTextBoxColumn col1 = new DataGridViewTextBoxColumn();
  col1.Name = "Project Name";
  col1.ValueType = typeof(string);
  dataGridView1.Columns.Add(col1);

  DataGridViewTextBoxColumn col2 = new DataGridViewTextBoxColumn();
  col2.Name = "Project ID";
  col2.ValueType = typeof(string);
  dataGridView1.Columns.Add(col2);

  DataGridViewComboBoxColumn col3 = new DataGridViewComboBoxColumn();
  col3.Name = "Associated Students";
  col3.ValueType = typeof(string);
  dataGridView1.Columns.Add(col3);

  for (int i = 0; i < this.Assignments.Count; i++)
  {
    DataGridViewRow row = (DataGridViewRow)(dataGridView1.Rows[0].Clone());

    DataGridViewTextBoxCell textCell = (DataGridViewTextBoxCell)(row.Cells[0]);
    textCell.ValueType = typeof(string);
    textCell.Value = this.Assignments[i].ProjectName;

    textCell = (DataGridViewTextBoxCell)(row.Cells[1]);
    textCell.ValueType = typeof(string);
    textCell.Value = this.Assignments[i].ProjectID;

    DataGridViewComboBoxCell comboCell = (DataGridViewComboBoxCell)(row.Cells[2]);
    comboCell.ValueType = typeof(string);
    comboCell.DataSource = this.Assignments[i].Associated_Students;

    dataGridView1.Rows.Add(row);
  }
}

Note: This will display what you are asking for but you will have to handle updating your data. I would suggest researching BindingList over List objects. There may be better solutions, but this worked quickly for me.

Community
  • 1
  • 1
OhBeWise
  • 5,350
  • 3
  • 32
  • 60