从数据源绑定不同条目的DataGridViewComboBoxCell [英] Bind different entries to DataGridViewComboBoxCell from a data source

查看:98
本文介绍了从数据源绑定不同条目的DataGridViewComboBoxCell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的数据我想在一个DataGridView显示:

  DataEntry []数据=新的[] {
        新DataEntry(){名称=A,条目=新的[] {1,2}},
        新DataEntry(){名称=B,条目=新[] {1,2,3}}};
 

名称将是一个简单的文本框字段,项组合框,其中的项目选择在列表中的元素。

因此​​,在这个例子中会有2行(下面是在DataGridView的样子):

 名称项

ROW1:A<选择1或2的制造>
ROW1:b将选择的1或2或3>
 

我的问题是,我怎么绑定这个数据?我在DataPropertyName,的DisplayMember和ValueMember属性看......但就是不能工作,这一个。

下面是code,因为它主张 - 与评论,我需要补充魔法几行来设置数据源等为条目列

 公共部分类Form1中:形态
    {
        DataEntry []数据=新的[] {
            新DataEntry(){名称=A,条目=新的[] {1,2}},
            新DataEntry(){名称=B,条目=新[] {1,2,3}}};

        公共Form1中()
        {
            的InitializeComponent();
            dataGridView1.AutoGenerateColumns = FALSE;

            VAR nameCol =新DataGridViewTextBoxColumn();
            nameCol.DataPropertyName =名称;

            VAR entriesCol =新DataGridViewComboBoxColumn();

                    // entriesCol。 ???? =条目; !

            dataGridView1.Columns.AddRange(新的DataGridViewColumn [] {nameCol,entriesCol});

            dataGridView1.DataSource =数据;
        }
    }


    公共类DataEntry
    {
        公共字符串名称{;组; }
        公开的IEnumerable<字符串>项{获得;组; }
    }
 

解决方案

请进今天上午,10分钟之内我有它的工作!

由于去<一href="http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/acab0014-269d-4501-9086-5f79081c9a77/"相对=nofollow>这个MSDN论坛的帖子

解决办法是订阅DataBindingComplete事件和随后经过的每一行,并设置每个ComboBoxCell数据源。这将是很好有一个更优雅的解决方案 - 但嘿 - !它的工作原理

下面是code样品我提交了原来的问题的工作版本:

 公共部分类Form1中:形态
{
    DataEntry []数据=新的[] {
        新DataEntry(){名称=A,条目=新[] {1,2,3,4}},
        新DataEntry(){名称=B,条目=新[] {1,2,3}}};


    字符串[] COLS =新的[] {COL1,COL2};

    公共Form1中()
    {
        的InitializeComponent();

        dataGridView1.AutoGenerateColumns = FALSE;

        VAR nameCol =新DataGridViewTextBoxColumn();
        nameCol.DataPropertyName =名称;

        VAR entriesCol =新DataGridViewComboBoxColumn();
        entriesCol.Name =条目;
        dataGridView1.Columns.AddRange(新的DataGridViewColumn [] {nameCol,entriesCol});

        dataGridView1.DataSource =数据;
        dataGridView1.DataBindingComplete + =新DataGridViewBindingCompleteEventHandler(dataGridView1_DataBindingComplete);
    }

    无效dataGridView1_DataBindingComplete(对象发件人,DataGridViewBindingCompleteEventArgs E)
    {
        的for(int i = 0; I&LT; dataGridView1.Rows.Count;我++)
        {
            的DataGridViewComboBoxCell comboCell =(的DataGridViewComboBoxCell)dataGridView1.Rows [I] .Cells [参赛作品];
            DataEntry进入= dataGridView1.Rows [I] .DataBoundItem为DataEntry;

            comboCell.DataSource = entry.Entries;
            comboCell.Value = entry.Entries.First();
        }
    }
}

公共类DataEntry
{
    公共字符串名称{;组; }
    公开的IEnumerable&LT;字符串&GT;项{获得;组; }
}
 

I have the following data I want to display in a DataGridView:

DataEntry[] data = new[]{
        new DataEntry(){Name = "A", Entries = new []{ "1", "2"}},
        new DataEntry(){Name = "B", Entries = new []{ "1", "2", "3"}}};

"Name" will be a simple TextBox field, and "Entries" a ComboBox where the items to choose from are the elements in the list.

So in this example there would be 2 rows (below is what the datagridview would look like):

       Name                         Entries

Row1 :    A                    <choice of "1" or "2">
Row1 :    B                    <choice of "1" or "2" or "3">

My question is, how do I bind this data?! I've looked at the DataPropertyName, DisplayMember and ValueMember properties... but just cannot work this one out.

Below is the code as it stands - with a comment where I need to add the magic couple of lines to set the DataSource etc. for the Entries column.

public partial class Form1 : Form
    {
        DataEntry[] data = new[]{
            new DataEntry(){Name = "A", Entries = new []{ "1", "2"}},
            new DataEntry(){Name = "B", Entries = new []{ "1", "2", "3"}}};

        public Form1()
        {
            InitializeComponent();
            dataGridView1.AutoGenerateColumns = false;

            var nameCol = new DataGridViewTextBoxColumn();
            nameCol.DataPropertyName = "Name";

            var entriesCol = new DataGridViewComboBoxColumn();

                    //entriesCol. ???? = "Entries"; !!

            dataGridView1.Columns.AddRange(new DataGridViewColumn[] { nameCol, entriesCol });

            dataGridView1.DataSource = data;   
        }
    }


    public class DataEntry
    {
        public string Name { get; set; }
        public IEnumerable<string> Entries { get; set; }
    }

解决方案

Come in this morning, and within 10 minutes I had it working!

Thanks go to this MSDN forum post

Solution is to subscribe to the "DataBindingComplete" event and to then go through each row and set the data source on each ComboBoxCell. It would be nice to have a more elegant solution - but hey - it works!

Below is a working version of the code sample I submitted in the original question:

public partial class Form1 : Form
{
    DataEntry[] data = new[]{
        new DataEntry(){Name = "A", Entries = new []{ "1", "2", "3", "4"}},
        new DataEntry(){Name = "B", Entries = new []{ "1", "2", "3"}}};


    string[] cols = new[] { "col1", "col2" };

    public Form1()
    {
        InitializeComponent();

        dataGridView1.AutoGenerateColumns = false;

        var nameCol = new DataGridViewTextBoxColumn();
        nameCol.DataPropertyName = "Name";

        var entriesCol = new DataGridViewComboBoxColumn();
        entriesCol.Name = "Entries";
        dataGridView1.Columns.AddRange(new DataGridViewColumn[] { nameCol, entriesCol });

        dataGridView1.DataSource = data;
        dataGridView1.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(dataGridView1_DataBindingComplete);
    }

    void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
    {
        for (int i = 0; i < dataGridView1.Rows.Count; i++)
        {
            DataGridViewComboBoxCell comboCell = (DataGridViewComboBoxCell)dataGridView1.Rows[i].Cells["Entries"];
            DataEntry entry = dataGridView1.Rows[i].DataBoundItem as DataEntry;

            comboCell.DataSource = entry.Entries;
            comboCell.Value = entry.Entries.First();
        }
    }
}

public class DataEntry
{
    public string Name { get; set; }
    public IEnumerable<string> Entries { get; set; }
}

这篇关于从数据源绑定不同条目的DataGridViewComboBoxCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆