开发者

C#: How to Display Binding Data with DataGridViewComboBoxColumn in DataGridView

I don't think it's a difficult problem. but I just cannot find / google the answer. Please help.

Basically, my app helps t开发者_如何学JAVAhe users to find a list of words (from a bunch of files), and the list of lists of files containing these words.

Say I have:

public class WordInfo
{
    public string Word { get; set; }
    public List<string> Files { get; set; }
}

And I have also created BindingList<WordInfo> from List<WordInfo>, and bound BindingList<WordInfo> as DataGridView.DataSource

I just don't know how to display WordInfo.Files with DataGridViewComboBoxColumn in DataGridView.

I googled a lot, it seems that I have to set:

DataGridViewComboBoxColumn cbxColumn = dgvWordList.Columns["Files"] as DataGridViewComboBoxColumn;
cbxColumn.DataSource = ??????; // How to get this data source from BindingList<WordInfo>
cbxColumn.DisplayMemeber = "DisplayMemeber"; // Can I have an example?
cbxColumn.ValueMember = "ValueMember"; // Can I have an example?

But I don't know how to set these properties. I googled, but the examples are too complicated.

Please help. thanks.

I think I have some problems understanding DataGridViewComboBoxColumn, and MSDN documentation has driven me crazy.

Peter


The thing is that I think you can't have just one BindingList of just Words...
You should define one BindingList<WordInfo> and bind Word property to Word column of the datagrid. then you should write just a little code in RowEnter or somewhere about when current row changed in order to bind Files list to that DataGridViewComboBoxColumn. This is what i did:



WordInfoCollection ww;

private void Form1_Load(object sender, EventArgs e)
{
    ww = new WordInfoCollection();

    //After filling data to this variable, 
    //you should set it as a BindignSource DataSource property
    wordsBindingSource.DataSource = ww;
}

private void wordsBindingSource_CurrentChanged(object sender, EventArgs e)
{
    if (wordsBindingSource.Current == null) return;

    clFiles.DataSource = ww[wordsBindingSource.Position].Files;
}

then you just need to bind your datagridview to wordsBindingSource...
Good luck


Solved!
Finally I found the answer... You dont need to use any event... just write this code after binding defenition code (maybe on form_Load)

int x = 0;
foreach (WordInfo word in ww)
{
    DataGridViewComboBoxCell dgCell = ((DataGridViewComboBoxCell)dgvWordList.Rows[x++].Cells["clFiles"]);
    dgCell.Items.AddRange(word.Files.ToArray());
}

Good luck my friend ;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜