Hey guys,

I have a datagridview called DataGridView1 which has four column each are combobox's, I need to be able to change the items in one of the comboboxes based on the previous one. For example

I have a column named FieldName which has the following values:

1.Product
2. ID

if the user selects products i want to be able to change a combobox called Value to "HomeProject","Work Stuff"

Would anyone have any ideas on how to do this.

If searched around and could not find anything...please help :S

Thanks in advance

Assuming you've defined each column of type DataGridViewComboBoxColumn, you'll want to add a handlers for the events: EditingControlShowing and CellBeginEdit. We use CellBeginEdit to identify the active cell and EditingControlShowing to obtain the edit control. The args DataGridViewCellCancelEventArgs and DataGridViewEditingControlShowingEventArgs provide us with the data access we need to set the dropdown list.

e.g

private void dgv_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
m_iActiveCellCol = e.ColumnIndex;
}


private void dgv_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox cbx;
if(m_iActiveCellCol ==2)
{
cbx = (ComboBox)e.Control;
cbx.DataSource = m_oCol2DataSource;
cbx.DisplayMember = oCol2DataSource.DisplayMember;
}
}

A much easier way in fact is to simple cast the particular column that you want to modify as the code below demonstrates:

DataGridViewComboBoxColumn col = (DataGridViewComboBoxColumn)dgv.Columns["Value"];
col.Items.Add("HomeProject");
col.Items.Add("Work Stuff");

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.