你好,我正在尝试做数据绑定,我有一个Form1类对象列表,我想绑定到一个数据网格视图,它看起来像:
public partial class Form1 : Form
{
public BindingList<PrefixDataAffichable> prefixepresent { get; set; }
public Form1()
{
InitializeComponent();
prefixepresent = new BindingList<PrefixDataAffichable>();
LoadData();
dataGridView1.DataSource = prefixepresent;
}
}
和prefixdataafficable类看起来像
public class PrefixDataAffichable
{
public string PrfixeInstance { get; set; }
public BindingList<string> PrfixePossibleChoice { get; set; }
public string PrefixeDescription { get; set; }
BindingList<string> PrfixePoPrfixeInstancessibleChoice = new BindingList<string>();
public PrefixDataAffichable(PrefixRef prefixref)
{
PrefixeDescription = prefixref.prefix._description;
PrfixeInstance = prefixref.prefix._prefixe + "(" + prefixref.texteentreparanthese + ")";
PrfixePossibleChoice.Add(_PrfixeInstance);
PrfixePossibleChoice.Add(_PrfixeInstance + "1!");
}
}
当我做数据绑定2列显示为textboxcolumn,但我不能使组合框列出现。我想手动创建它,并将DataPropertyName手动设置为:prfixepositecice,但程序在运行时崩溃。
有谁知道当我选择类作为数据绑定源时如何使列出现,或者如何通过代码添加列;
更新:我添加了代码
dataGridView1.DataSource = prefixepresent;
DataGridViewComboBoxColumn colbox = new DataGridViewComboBoxColumn();
colbox.DataPropertyName = "PrfixePossibleChoice";
dataGridView1.Columns.Add(colbox);
错误是:System。ArgumentException: DataGridViewComboBoxCell值不合法
在绑定组合框列时需要为它设置DataSource
。您正在获得错误,因为为组合框单元格设置的值在组合框列的DataSource
或Items
集合中不可用。
dataGridView1.DataSource = prefixepresent;
DataGridViewComboBoxColumn colbox = new DataGridViewComboBoxColumn();
colbox.DataPropertyName = "PrfixePossibleChoice";
colbox.DataSource = set the datasource here
dataGridView1.Columns.Add(colbox);