0

I'm working on a scheduling program, and inside the dataGridView, we have a few ComboBox Columns that are populated by 3 entries upon creation, but I wanted to be able to add more as the user creates them, but I have no idea how you would access the combobox data. Any help is appreciated!

// this is initialized in a separate part.
/* System::Windows::Forms::DataGridView^ dataGridView;*/

System::Windows::Forms::DataGridViewComboBoxColumn^ newCol = 
    (gcnew System::Windows::Forms::DataGridViewComboBoxColumn());

dataGridView->Columns->AddRange(gcnew cli::array< System::Windows::Forms::DataGridViewComboBoxColumn^  >(1) {newCol});  

// add the choices to the boxes.
newCol->Items->AddRange("User inputted stuff", "More stuff", "Add New..."); 
Ralis
  • 15
  • 4
  • I'm puzzled. It's not the **user**, that creates entries. It's **your application** that does, on behalf of the user. I have no clue, how you can have implemented the functionality, and now ask for help with implementing the very same functionality. What am I missing? – IInspectable Jun 04 '15 at 11:59
  • Inside the columns, there are the drop-down comboboxes, and when the user selects "Add New", they're presented with a textbox that requests a new input. When it's inputted, however, I don't know how to add that into the combobox for them to select in the future. – Ralis Jun 04 '15 at 12:03
  • Show how you add items in the `ComboBoxColumn` in the begining – Fabio Jun 04 '15 at 12:40
  • @Fabio I added the requested code. – Ralis Jun 04 '15 at 12:54

1 Answers1

1

Solution

If you have access to the data from the user entry and you know the column index for the DataGridViewComboBoxColumn, you should be able to just do the following wherever needed:

DataGridViewComboBoxColumn^ comboboxColumn = dataGridView->Columns[the_combobox_column_index];

if (comboboxColumn != nullptr)
{
    comboboxColumn->Items->Add("the new user entry");
}

Comments Response

how could you change the selected index of that combobox (the one that the edit was triggered on)? [...] we want it so that when the new item is added the selected index is set to that new item).

Couple of ways come to mind.

  1. Add a single line within the if-statement of the above code. This will set the default displayed value for each DataGridViewComboBoxCell in the DataGridViewComboBoxColumn.

    if (comboboxColumn != nullptr)
    {
        comboboxColumn->Items->Add("the new user entry");
        comboboxColumn->DefaultCellStyle->NullValue = "the new user entry";
    }
    
    • Pros: Clean, efficient. Previous user-selected values are left intact. The cell's FormattedValue will display the new user value by default if no other selection has been made.
    • Cons: Doesn't actually set a cell's selected value, so Value will return null on cells not explicitly user-selected.
  2. Actually set the value of certain cells (based on your criteria) to the user-added value.

    if (comboboxColumn != nullptr)
    {
        comboboxColumn->Items->Add("the new user entry");
    
        for (int i = 0; i < dataGridView->Rows->Count; i++)
        {
            DataGridViewComboBoxCell^ cell = dataGridView->Rows[i]->Cells[the_combobox_column_index];
    
            if ( cell != nullptr /* and your conditions are met */ )
            {
                cell->Value = "the new user entry";
            }
        }
    }
    
    • Pros: The Value of targeted cells is actually set to the new user value.
    • Cons: Logic deciding which cells should be affected is more complicated.
OhBeWise
  • 5,350
  • 3
  • 32
  • 60
  • Thanks for the answer first off. Now, given that `comboboxColumn` how could you change the selected index of that combobox (the one that the edit was triggered on)? (I know this isn't really part of the question but i'm working on the project with Ralis and we want it so that when the new item is added the selected index is set to that new item) – Stephen Buttolph Jun 04 '15 at 23:20
  • @StephenB Edited my answer to address your additional questions. Hope that helps. Let me know if I misinterpreted anything. – OhBeWise Jun 05 '15 at 16:37
  • Thank you very much I think that is exactly what we needed. I'll make sure Ralis accepts this answer when I see him Monday. Thanks again! – Stephen Buttolph Jun 05 '15 at 18:32