473,396 Members | 1,968 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

DataGridViewComboBox: allow Value that is NOT in Items list

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.
Nov 3 '08 #1
2 14694
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

DrDBF wrote:
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.
Nov 3 '08 #2
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.

"Ashutosh Bhawasinka" wrote:
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
Nov 3 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
by: n00m | last post by:
import socket, thread host, port = '192.168.0.3', 1434 s1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s2.connect((host, 1433))...
32
by: jamieexley | last post by:
what are the parameters needed in the constructor call below to create a Date variable whose intitial value is the 15th of March 2004????
2
by: Sehboo | last post by:
I am trying to use key value pair list, but I don't want to use sorted list because it messes up my order. I am not sure what other options I have. Can anybody point? Thanks
4
by: semomaniz | last post by:
I have a list box that has a list of roles defined. List of roles are binded to the list box in page load event. I am trying to retrieve the selected role from the text box but i am getting a null...
7
vikas251074
by: vikas251074 | last post by:
Can I assign value to cookies immediately after selecting a value from list? <select name="vlan_name" style="width:150px "> <%set rs = conn.execute("select vlan_name from vlan_master order by...
1
by: AarthiVedhavalli | last post by:
Hi, In my C# web application I have a list box whic is binded to database. What i need now is , if i double click a value in list box that vale should be added to a dropdown list.. Any...
1
by: libish | last post by:
can we add a value to a list item?? i got a list with some list members here can we add a value to that list item??? ie. suppose a list contains items like "item1" "item2" "item3" etc...
0
vikas251074
by: vikas251074 | last post by:
I am using Oracle 9i and ASP I have empno, empname, designation, category, dob. Category have two option 'S' or 'O' When I enter empno, empname, designation, category and dob and when I...
0
by: bullfrog83 | last post by:
I'm working in an Access project (.adp) that's linked to a SQL Server db. What I can't get to work is the Allow Value List Edits option for a combobox. It works fine in a .accdb file. However, even...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.