0

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");
        }
}
user2330632
  • 15
  • 1
  • 5

1 Answers1

0

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");
        }
    }
}
Abhishek
  • 2,925
  • 4
  • 34
  • 59
  • Thanks for reply. Should I keep this as is? `table_assets.CellClick += new DataGridViewCellEventHandler(comboBoxColumn_AssetsOwner_Selected);` - inside Client(). – user2330632 Jan 12 '15 at 00:41
  • You said "I have been unable to add an event handler to the ComboBox". So instead of that event, you could just add my code. In place of SelectedIndexChanged, you could add your code. Ill modify my answer. – Abhishek Jan 12 '15 at 00:46
  • Oh I understand now, but your code is an event handler correct? So it would have to be initialized with something like this, right?: `table_assets.CellClick += new DataGridViewCellEventHandler(comboBoxColumn_AssetsOwner_Selected);` But for me, that is giving an error: `Error 1 No overload for 'comboBoxColumn_AssetsOwner_Selected' matches delegate 'System.Windows.Forms.DataGridViewCellEventHandler'` – user2330632 Jan 12 '15 at 00:55