C# 将具有不同数据源的DataGridViewComboxCell添加到DataGridView

C# 将具有不同数据源的DataGridViewComboxCell添加到DataGridView,c#,winforms,datagridview,datagridviewcombobox,C#,Winforms,Datagridview,Datagridviewcombobox,我已经检查了其他帖子,但仍然无法修复我的问题: 我有两种表格: 创建客户端的一个(可以有多个电话、地址和汽车) 应显示每个客户端信息的主窗体 首先,是否有可能在每一行中都有一个客户机,以及包含每个客户机信息的三个组合(电话、地址和汽车) 例如: 我有两个客户,John和Jack,我希望行显示如下: 0-约翰-约翰.电话-约翰.地址-约翰.汽车 1-杰克-杰克。电话-杰克。地址-杰克。汽车 另外,我不知道应该向行传递什么作为combobox的参数,应该传递数据源、项还是combo本身?无论我

我已经检查了其他帖子,但仍然无法修复我的问题: 我有两种表格:

  • 创建客户端的一个(可以有多个电话、地址和汽车)
  • 应显示每个客户端信息的主窗体

首先,是否有可能在每一行中都有一个客户机,以及包含每个客户机信息的三个组合(电话、地址和汽车)

例如:

我有两个客户,John和Jack,我希望行显示如下:

0-约翰-约翰.电话-约翰.地址-约翰.汽车
1-杰克-杰克。电话-杰克。地址-杰克。汽车

另外,我不知道应该向行传递什么作为combobox的参数,应该传递数据源、项还是combo本身?无论我通过什么组合框,我都会得到:
System.ArgumentException:DatagridViewRowComboBoxCell值无效

这是实际的代码

private DataGridViewRow BuildRow(Cliente cliente)
{
    DataGridViewRow row = new DataGridViewRow();           
    DataGridViewComboBoxCell Autos = new DataGridViewComboBoxCell();


    Autos.DataSource = cliente.Autos;
    Autos.ValueMember = cliente.Autos[0].ToString();
    row.CreateCells(DGCliente, cliente.Codigo.ToString(), cliente.Nombre, Autos);
    row.Tag = cliente;
    DGCliente.Rows.Add(row);

    return row;
}
客户端类代码:(西班牙语)

公共类客户
{
公共字符串Nombre{get;set;}
公共Int32 Codigo{get;set;}
公共列表自动{get;set;}
公共列表目录{get;set;}
公共列表电话{get;set;}
公共客户()
{
this.Direcciones=新列表();
this.Telefonos=新列表();
this.Autos=新列表();
}
公共客户(字符串名称,Int32 Codigo)
{
这个.Nombre=Nombre;
这个.Codigo=Codigo;
this.Direcciones=新列表();
this.Telefonos=新列表();
this.Autos=新列表();
}
公共无效AgregarTelefono(布尔esCelular、字符串编号、字符串区域)
{ 
新增(新电信号(esCelular、numero、area));
}
public void agregardicion(字符串调用者、字符串altura、字符串localidad、字符串provincia)
{
添加(新目录(calle、altura、localidad、provincia));
}
公共无效AgregarAuto(自动)
{
this.Autos.Add(自动);
}
}

关于你的第一个问题。你当然可以。您可以查看或查看带有该标记的任何其他StackOverflow

真正的问题是如何。大多数帖子都与WPF相关,在这种情况下,绑定会有所帮助,但我强烈建议大家通读一下,他有一个类似结果的代码示例

苏尔特:)

编辑:
您的
DataGridViewComboxCell
源应为
IList
IListSource
,其中包含用于向下拉列表提供数据的值集合(如果您不相信,请查看)

因此,每个对象都应该有一个属性,它是
列表
或满足
IList
接口的任何属性,并且应该将其绑定到源代码

因此,例如,如果下面的类是您的对象,那么您应该将该对象绑定到行,但特别是绑定到DataGridViewComboxCell源的该1(&2)(就像他在示例中使用
dtTitles
所做的那样)

公共类MyObject
{
公共列表BindToThis1{get;set;}
公共列表BindToThis2{get;set;}
公共对象()
{
BindToThis1=新列表{“你好”,“世界”};
BindToThis2=新列表{“红色”、“蓝色”、“绿色”};
}
}

您可以使用两种类型的单元格(文本框单元格和组合框单元格)为每个客户端创建一行。 考虑以下事项:

 public class PersonRow : DataGridViewRow
    {
        public DataGridViewTextBoxCell Name;
        public DataGridViewComboBoxCell Phones;
        public DataGridViewComboBoxCell Cars;

        public PersonRow(Person person)
        {
            Name.Value = person.Name;

            DataGridViewComboBoxCell phones = new DataGridViewComboBoxCell();
            phones.Items.AddRange((DataGridViewComboBoxCell.ObjectCollection)person.Phones); //add the items from Person.Phones to PersonRow.Phones combobox cell

            Phones = phones;

            DataGridViewComboBoxCell cars = new DataGridViewComboBoxCell();
            cars.Items.AddRange((DataGridViewComboBoxCell.ObjectCollection)person.Cars); //add Person.Cars to PersonRow.Cars combobox cell 

            Cars = cars;

            Cells.AddRange(new DataGridViewCell[] { Name, Phones, Cars }); //add cells to the row

        }
    }

public class Person
    {
        public string Name { get; set; }

        public IList<Phones>  Phones { get; set; } //be sure to use the IList interface
        public IList<Cars> Cars { get; set; } //be sure to implement the IList interface

        public Person( string name, List<Phones> phones, List<Cars> cars)
        {
            Name = name;
            Phones = phones;
            Cars = cars;
        }
    }

public class Phones
    {
        public string Name { get; set; }
        public string Model { get; set; }

        public Phones(string name, string model)
        {
            Name = name;
            Model = model;

        }
    }

    public class Cars
    {
        public string Name { get; set; }
        public string Model { get; set; }

        public Cars(string name, string model)
        {
            Name = name;
            Model = model;
        }
    }
公共类PersonRow:DataGridViewRow
{
公共DataGridViewTextBoxCell名称;
公共DataGridViewComboBox手机;
公共DataGridViewBoxCell汽车;
公众人士行(人)
{
Name.Value=person.Name;
DataGridViewComboxCellphones=新的DataGridViewComboxCell();
phones.Items.AddRange((datagridviewcomboxcell.ObjectCollection)person.phones);//将person.phones中的项目添加到PersonRow.phones组合框cell
电话=电话;
DataGridViewComboxCell cars=新DataGridViewComboxCell();
cars.Items.AddRange((DataGridViewComboBoxCell.ObjectCollection)person.cars);//将person.cars添加到PersonRow.cars组合框单元格
汽车=汽车;
Cells.AddRange(新DataGridViewCell[]{Name,Phones,Cars});//将单元格添加到行中
}
}
公共阶层人士
{
公共字符串名称{get;set;}
公共IList电话{get;set;}//请确保使用IList接口
公共IList Cars{get;set;}//请确保实现IList接口
公众人物(字符串名称、电话列表、汽车列表)
{
名称=名称;
电话=电话;
汽车=汽车;
}
}
公用电话
{
公共字符串名称{get;set;}
公共字符串模型{get;set;}
公用电话(字符串名称、字符串型号)
{
名称=名称;
模型=模型;
}
}
公车
{
公共字符串名称{get;set;}
公共字符串模型{get;set;}
公共汽车(字符串名称、字符串型号)
{
名称=名称;
模型=模型;
}
}
还包括结帐: 及
希望这有帮助

谢谢,但我的问题是,我需要不同的数据源用于不同的组合框,而博客中的示例只有一个,并且是针对整个专栏的。我还需要用我的客户端数据创建一行,我不知道要传递什么参数。我已经这样做了,客户端类有3个属性,它们是集合,(列表,和),但仍然无法解决我的问题。如果方法已经添加了行,为什么要返回该行(如果需要,反之亦然)?此外,缺少变量的命名。什么是
Autos
?您可以将其用作
DataGridViewComboxCell
(简称DGVCBC),也可以将其用作客户机上的属性
public class MyObject
{
    public List<string> BindToThis1 { get; set; }
    public List<string> BindToThis2 { get; set; }

    public MyObject()
    {
        BindToThis1 = new List<string> { "hello", "world" };
        BindToThis2 = new List<string> { "red", "blue", "green" };
    }
}
 public class PersonRow : DataGridViewRow
    {
        public DataGridViewTextBoxCell Name;
        public DataGridViewComboBoxCell Phones;
        public DataGridViewComboBoxCell Cars;

        public PersonRow(Person person)
        {
            Name.Value = person.Name;

            DataGridViewComboBoxCell phones = new DataGridViewComboBoxCell();
            phones.Items.AddRange((DataGridViewComboBoxCell.ObjectCollection)person.Phones); //add the items from Person.Phones to PersonRow.Phones combobox cell

            Phones = phones;

            DataGridViewComboBoxCell cars = new DataGridViewComboBoxCell();
            cars.Items.AddRange((DataGridViewComboBoxCell.ObjectCollection)person.Cars); //add Person.Cars to PersonRow.Cars combobox cell 

            Cars = cars;

            Cells.AddRange(new DataGridViewCell[] { Name, Phones, Cars }); //add cells to the row

        }
    }

public class Person
    {
        public string Name { get; set; }

        public IList<Phones>  Phones { get; set; } //be sure to use the IList interface
        public IList<Cars> Cars { get; set; } //be sure to implement the IList interface

        public Person( string name, List<Phones> phones, List<Cars> cars)
        {
            Name = name;
            Phones = phones;
            Cars = cars;
        }
    }

public class Phones
    {
        public string Name { get; set; }
        public string Model { get; set; }

        public Phones(string name, string model)
        {
            Name = name;
            Model = model;

        }
    }

    public class Cars
    {
        public string Name { get; set; }
        public string Model { get; set; }

        public Cars(string name, string model)
        {
            Name = name;
            Model = model;
        }
    }