DataGridViewComboBox: allow Value that is NOT in Items list

D

DrDBF

I understand that the Value put into a DataGridViewComboBoxCell has to be a
member of the Items list or an exception is thrown.

I also understand that you can override that exception by handling the
DataError event and just doing nothing in the circumstance that caused the
error.

Here's my situation: I would like to allow the user to EITHER select an item
from the ComboBox (in a DataGridView) OR type in a new value of their own.

I've handled the EditingControlShowing event to change the DropDownStyle of
the editing control to DropDown (rather than DropDownList). This allows me to
type something new into the edit box of the ComboBox.

Then I handled the CellEndEdit event to snag the Text from the editing
ComboBox control. I take that Text and put it into the Value of the
DataGridViewComboBoxCell. This triggers the exception.

If I override that exception, things move along OK, except that that cell
now takes the value of the first item in the ComboBox Items list.

Is there a way to force the ComboBox to accept the new value?

Or perhaps a different way to work around this:
Is there a way to have the DataGridView column be of the Text type, but
attach a ComboBox instead of a TextBox for editing. Then take whatever the
Text value is from the ComboBox EditingControl and put that into the
DataGridViewTextBoxCell?

I hope: 1) this makes sense, and 2) someone out there knows how to pull this
off.

Thanks.
 
D

DrDBF

Sorry, I forgot to mention that it is NOT actually data bound. I fill the
Items list manually. I'll grant that I am using the results from a
DataReader, but I manually loop through the DataReader
while (reader.Read())
{ dgvCo.Items.Add(reader.GetString(0)) }

And while I think your solution would work, I don't actually want to save
the Value that the user types. To be more specific on what I'm trying to
accomplish.... The DataGridView is for entering an invoice. Most of the
invoice items have a specific "name" but I want to possibility of a "Misc"
invoice item into which the user can type whatever description of that
invoice line item they want. I don't want or need this saved into the
database of invoice items since it will vary with each invoice.
 
A

Ashutosh Bhawasinka

Your combo box is using an data source, so you definitely have a
table(or another source) associated with it. Jut add that value as new
row in that table before the DataGridView validates the cell content.
You can try to add the value in the table when the user has finished
editing.
Validation event is fired after the end-editing(or whatever) event is fired.

Once you add the row in the underlying table, the validation will/should
succeed.

I have not tried this, but it should definitely work. I will try it out
tomorrow.

Thanks & Regards,
Ashutosh Bhawasinka
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top