DataGridView ComboBox Problem


New member
Feb 2, 2012
Programming Experience
I have a DataGridView containing information of cities of the world. User can retreive records and insert record of cities from DataGridView. Since every city is associated with a country, therefore in order to insert a new city user must insert country information too.
For this purpose, i have created four columns in DataGridView dynamically:
1 - S.No.
2 - Country(combo box)
3 - City Code (Edit Box)
4 - City Name (Edit box)
I have created the 'Country' column in Grid using "DataGridViewComboBoxColumn" class and populated it with DataTable using DataSource property, and hence the DisplayMember is 'Country_Name' and ValueMember is 'Country_Code' and it is working fine at the moment. Whenever user wants to add a new city, obviously user has to select the country too which i have provided in combobox.
Now the problem is on retreiving data. I am populating the Grid manually i.e. looping row by row and inserting into each cell from a DataSet which is again working fine for all columns EXCEPT the Country column of Grid. The program gives me an exception repeatedly stating that "System.ArgumentException: DataGridViewComboBoxCell value is not valid.". I am trying to populate the Country column cell with the 'Country_Code' hoping to get the 'Country_Name' as the 'country_name' is the ValueMember BUT so far it is not letting me do that.
Kindly help me out with this problem, ill be thankful to you.

you can easily solve your problem just visit this tutorial page and look for what you need.
there are many tutorials relate to .net grid with code.
Dapfor .Net Grid Tutorial

Given that that doesn't relate to the DataGridView, I'm not sure it's relevant.

Don't populate the grid manually. Use data-binding. Bind one table to the combo box column and the other table to the grid.
Sounds like you want the changes to be committed as soon as the user changes the drop down box, without them having to click off of the cell. In order to do this you will need to force the commit when the change happens using commit.edit. Then you could just listen for the Cell Value Changed and avoid having to try and register for the Combo Box Value Changed event on the underlying editing control.
.Net Grid tutorial Part2: Data Binding

Latest posts

Top Bottom