views:

40

answers:

2

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

Basically, my app helps the 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

A: 

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

Dr TJ
you know, you should just bind your files list variable when the current word is bound. I mean, first (in form_load or when ever) just bind `Word` property of the DataGridView to the Word property of the `BindingList`. then, when current record is changing (in RowEnter or RowLeave), you should bind your Files list to the files column of that datagrid.
Dr TJ
I'm actually using SortableBindingList<T> (inherits from BindingList<T>). There is no CurrentChanged event for BindingList<T>.
Peter Lee
another thing is that, you should create a new class or structure for files list in order to be able to handle ValueMember and DisplayMember of that combo box... let me see what can i do...
Dr TJ
you can use `RowEnter` and `RowLeave` events of the DataGridView instade.
Dr TJ
A: 

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

Dr TJ