I have a DataGridView called table_assets which is created in the designer.
In runtime, a DBAdapter is attached to the DataSource of table_assets, which populates table_assets with columns - one of the columns [Headertext] is: Owner.
The column Owner is a column of ComboBoxs.
A requirement of this program (along with the above) is that Items in the ComboBoxs that are inside the Column Owner, in all currently used Rows has to change from:
<client>
to
<client>
<spouse>
Joint
When the global Boolean spouseActive is false or true, respectably.
The challenge I am having is telling the program to change the Items. For the most part, I have been unable to add an event handler to the ComboBox's which as I understand it, is the only way to change the Items.
Here is my relevant code, although I do not think it would be of much use - it will crash in comboBoxColumn_AssetsOwner_Selected:
bool spouseActive;
public Client()
{
// table_assets
assetsAdapter = new DBAdapter(database.clientAssets);
assetsAdapter.ConstructAdaptor(null, " cID = " + clientID.ToString());
table_assets.DataSource = (DataTable)assetsAdapter.ReturnTable();
table_assets.CellClick += new DataGridViewCellEventHandler(comboBoxColumn_AssetsOwner_Selected);
}
private void comboBoxColumn_AssetsOwner_Selected(object sender, EventArgs e)
{
DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)sender;
if (spouseActive == true)
{
cell.Items.Add("<spouse>");
cell.Items.Add("Joint");
Debug.WriteLine("added");
}
else
{
cell.Items.Remove("<spouse>");
cell.Items.Remove("Joint");
Debug.WriteLine("removed");
}
}
Try using EditingControlShowing
event for the DataGridView
:
bool spouseActive;
public Client()
{
// table_assets
assetsAdapter = new DBAdapter(database.clientAssets);
assetsAdapter.ConstructAdaptor(null, " cID = " + clientID.ToString());
table_assets.DataSource = (DataTable)assetsAdapter.ReturnTable();
table_assets.EditingControlShowing += table_assets_EditingControlShowing;
}
private void table_assets_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is ComboBox)
{
ComboBox cbMyComboBox = e.Control as ComboBox;
if (spouseActive == true)
{
cbMyComboBox.Items.Add("<spouse>");
cbMyComboBox.Items.Add("Joint");
Debug.WriteLine("added");
}
else
{
cbMyComboBox.Items.Remove("<spouse>");
cbMyComboBox.Items.Remove("Joint");
Debug.WriteLine("removed");
}
}
}