Windows Develop Bookmark and Share   
 index > Windows Forms General > DataGridView with row specific combo box contents
 

DataGridView with row specific combo box contents

Hi,

my problem concerns a DataGridView with a DataGridViewComboboxColumn. I want to display different contents in each combobox of this column. How can I bounddifferentlists of strings to the cells of the column?

The content of DataGridViewComboboxColumn.Items or DataGridViewComboboxColumn.DataSource always shows up in all comboboxes.

Thanks for help.

Stefan

.Stefan.  Tuesday, August 22, 2006 11:10 AM

You may prepared several datasources for each combobox,e.g, DS1,DS2...DSn,as well as a “collection datasource" which is the combined with DS1~DSn. Then change the datasource of cell being edit, after finishing edition, change the datasource back to CollectionDS

private void dataGridView1_CellBeginEdit(object sender,   DataGridViewCellCancelEventArgs e)
{
    if (e.ColumnIndex == this.ComboBoxColumn.Index)
    {
        // Set the combobox cell datasource to corresponding BindingSource
        DataGridViewComboBoxCell dgcb = (DataGridViewComboBoxCell)dataGridView1
      [e.ColumnIndex, e.RowIndex];
        dgcb.DataSource = ds[e.RowIndex];
    }
}

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == this.ComboBoxColumn.Index)
    {
        // Reset combobox cell to the collection BindingSource
        DataGridViewComboBoxCell dgcb = (DataGridViewComboBoxCell)dataGridView1
      [e.ColumnIndex, e.RowIndex];
        dgcb.DataSource = CollectionDS;
    }
}

Wang Chi  Tuesday, August 22, 2006 12:41 PM

You may prepared several datasources for each combobox,e.g, DS1,DS2...DSn,as well as a “collection datasource" which is the combined with DS1~DSn. Then change the datasource of cell being edit, after finishing edition, change the datasource back to CollectionDS

private void dataGridView1_CellBeginEdit(object sender,   DataGridViewCellCancelEventArgs e)
{
    if (e.ColumnIndex == this.ComboBoxColumn.Index)
    {
        // Set the combobox cell datasource to corresponding BindingSource
        DataGridViewComboBoxCell dgcb = (DataGridViewComboBoxCell)dataGridView1
      [e.ColumnIndex, e.RowIndex];
        dgcb.DataSource = ds[e.RowIndex];
    }
}

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == this.ComboBoxColumn.Index)
    {
        // Reset combobox cell to the collection BindingSource
        DataGridViewComboBoxCell dgcb = (DataGridViewComboBoxCell)dataGridView1
      [e.ColumnIndex, e.RowIndex];
        dgcb.DataSource = CollectionDS;
    }
}

Wang Chi  Tuesday, August 22, 2006 12:41 PM

It works perfect.

Thank you!

.Stefan.  Tuesday, August 22, 2006 2:59 PM

You can use google to search for other answers

Custom Search

More Threads

• I changed the form design but on run mode its showing the old form designing not getting updated?
• how to move control at runtime
• How many projects should I use for my solution?
• Add a line break in a Label.Text
• datagrid
• disable text formatting for button.text?
• DataGrid
• DatagridView with radiobutton + text.
• Scaling Windows Forms
• How to Receive Information, submitted by a user