Winforms 基本上,我有一个windows窗体中的数据网格视图。我添加了一个组合框作为列

Winforms 基本上,我有一个windows窗体中的数据网格视图。我添加了一个组合框作为列,winforms,data-binding,datagridview,combobox,Winforms,Data Binding,Datagridview,Combobox,这是我的对象结构 class object { string projectname; string projectid; list<string> associated_students; } 类对象 { 字符串projectname; 弦投影; 列出相关的大学学生; } //我绑定到网格的列表 list<objects> objectList

这是我的对象结构

       class object 
       {
           string projectname;
           string projectid;
           list<string> associated_students;
       }
类对象
{
字符串projectname;
弦投影;
列出相关的大学学生;
}
//我绑定到网格的列表

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

现在,我想将datagrid中的组合框与列表“associated_students”绑定起来。

如果我理解了这个问题,您希望每一行都与对象列表中的一个对象绑定,并且希望第三列显示该对象的唯一关联学生列表的组合框。如果我是正确的,简单的搜索会导致类似的问题:

要解决此问题,需要手动绑定每一行。我能够复制您的问题,并提出了以下解决方案:

你的类“对象”

公共类赋值
{
公开转让()
{
this.Associated_Students=新列表();
}
公共字符串ProjectName{get;set;}
公共字符串ProjectID{get;set;}
公共列表关联_学生{get;set;}
}
表格1:

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);
  }
}
public Form1()
{
初始化组件();
this.Assignments=新列表()
{
新任务()
{
projectd=“1”,
ProjectName=“First”,
Associated_Students=新列表(){“我”、“你”、“他”、“她”}
},
新任务()
{
projectd=“2”,
ProjectName=“秒”,
Associated_Students=新列表(){“Foo”,“Bar”}
}
};
this.BindDGViewToList();
}
公共列表分配{get;set;}
public void BindDGViewToList()
{
DataGridViewTextBoxColumn col1=新的DataGridViewTextBoxColumn();
col1.Name=“项目名称”;
col1.ValueType=typeof(字符串);
dataGridView1.Columns.Add(col1);
DataGridViewTextBoxColumn col2=新的DataGridViewTextBoxColumn();
col2.Name=“项目ID”;
col2.ValueType=typeof(字符串);
dataGridView1.Columns.Add(col2);
DataGridViewComboBoxColumn col3=新的DataGridViewComboxColumn();
col3.Name=“联系学生”;
col3.ValueType=typeof(字符串);
dataGridView1.Columns.Add(col3);
对于(int i=0;i
注意:这将显示您要求的内容,但您必须处理数据更新。我建议研究BindingList而不是List对象。也许有更好的解决办法,但这对我来说很快就奏效了

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);
  }
}