944,150 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 31098
  • C# RSS
Jun 4th, 2007
0

Changing a datagridview's combobox items based on another combobox

Expand Post »
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

Thanks in advance
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mstester is offline Offline
17 posts
since Aug 2006
Nov 6th, 2007
0

Re: Changing a datagridview's combobox items based on another combobox

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dox is offline Offline
2 posts
since Nov 2007
Nov 6th, 2007
0

Re: Changing a datagridview's combobox items based on another combobox

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dox is offline Offline
2 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: help in Coding required of ASP.net
Next Thread in C# Forum Timeline: form level variable





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC