繁体   English   中英

数据绑定datagridview组合框列和文本框

[英]databinding datagridview comboboxcolumn and textbox

您好,我正在尝试进行数据绑定,我在Form1类中有一个对象列表,我想将其绑定到看起来像这样的datagrid视图:

public partial class Form1 : Form
{

   public BindingList<PrefixDataAffichable> prefixepresent { get; set; }

   public Form1()
   {
      InitializeComponent();
      prefixepresent = new BindingList<PrefixDataAffichable>();
      LoadData();
      dataGridView1.DataSource = prefixepresent;
    }


}

和PrefixDataAffichable类看起来像

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手动设置为:PrfixePossibleChoice但是,程序在运行时会崩溃。

有没有人知道当我选择类作为数据绑定源时如何使列出现,或如何通过代码添加列;

更新 :我添加了代码

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM