c++ - Accessing a Combobox inside a dataGridView Column? -
i'm working on scheduling program, , inside datagridview, have few combobox columns populated 3 entries upon creation, wanted able add more user creates them, have no idea how access combobox data. appreciated!
// initialized in 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 choices boxes. newcol->items->addrange("user inputted stuff", "more stuff", "add new...");
solution
if have access data user entry , know column index datagridviewcomboboxcolumn
, should able following wherever needed:
datagridviewcomboboxcolumn^ comboboxcolumn = datagridview->columns[the_combobox_column_index]; if (comboboxcolumn != nullptr) { comboboxcolumn->items->add("the new user entry"); }
comments response
how change selected index of combobox (the 1 edit triggered on)? [...] want when new item added selected index set new item).
couple of ways come mind.
add single line within
if-statement
of above code. set default displayed value eachdatagridviewcomboboxcell
indatagridviewcomboboxcolumn
.if (comboboxcolumn != nullptr) { comboboxcolumn->items->add("the new user entry"); comboboxcolumn->defaultcellstyle->nullvalue = "the new user entry"; }
- pros: clean, efficient. previous user-selected values left intact. cell's
formattedvalue
display new user value default if no other selection has been made. - cons: doesn't actually set cell's selected value,
value
returnnull
on cells not explicitly user-selected.
- pros: clean, efficient. previous user-selected values left intact. cell's
actually set value of cells (based on criteria) user-added value.
if (comboboxcolumn != nullptr) { comboboxcolumn->items->add("the new user entry"); (int = 0; < datagridview->rows->count; i++) { datagridviewcomboboxcell^ cell = datagridview->rows[i]->cells[the_combobox_column_index]; if ( cell != nullptr /* , conditions met */ ) { cell->value = "the new user entry"; } } }
- pros:
value
of targeted cells actually set new user value. - cons: logic deciding which cells should affected more complicated.
- pros:
Comments
Post a Comment