954,546 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Changing a datagridview's combobox items based on another combobox

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

mstester
Newbie Poster
17 posts since Aug 2006
Reputation Points: 10
Solved Threads: 0
 

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

dox
Newbie Poster
2 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

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

dox
Newbie Poster
2 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You