fweeee

Hello,

A quick overview of my issue. I have three tables:

Item - Primary Key is "ID", of type int.

Vendor - Primary Key is "ID" of type int.

ItemVendor - Primary key contains two fields - "ItemID" and "VendorID" - both of type int. Nulls are not permitted.

I have created a relationship between ItemVendor and the other two tables in SQL Server Management.


I have created a new form with a datagrid on it to show/edit the ItemVendor table. This form shows both the fields (ItemID and VendorID). I have edited the columns so they are of type "DataGridViewComboBoxColumn", and they show the Item and Vendor names (from the respective tables) rather than the ID, and the combo box shows the available names rather than ID's. (I hope I havnt lost anyone at this stage).


Now, if I populate the data manually in SQL Server Management, it works fine, and shows me the names of the vendors/items just fine when i run the form. But as soon as I try to insert a new record using the form, I get an error telling me "System.ArgumentException: DataGridViewComboBoxCell value is not valid.".


I suspect this has to do with the fact that when I first create a new record, the two fields (ItemID and VendorID) are initialised to zero (as I am not allowing nulls). The properties of the comboboxes will try to to find the names for the vendor/item selected, and fail.


I'm thinking that what I'm trying to do isnt that rare, so I must be going about it the wrong way. Can anyone offer some assistance here Maybe tell me a better way to do it


I'm not sure if this is relevant, but I'm using codesmith/nettiers in this project.


Thanks.



Re: Windows Forms Data Controls and Databinding combobox giving an error when field is null

Suprotim Agarwal

Hi,

Does the error come when you close the form Without seeing some code, it becomes difficult to analyze the problem

Check this post to see if it helps

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=2040712&SiteID=1

HTH,
Suprotim Agarwal

-----
http://www.dotnetcurry.com
-----








Re: Windows Forms Data Controls and Databinding combobox giving an error when field is null

fweeee

No - closing the form is not an issue at all. Displaying records is just fine (it displays the vendor and item names, not the ID, as I want it to).
It is only when I go to insert a new line (click on the line with an * next to it), that I get the error.

The only thing that makes sense to me, is that the vendor combobox are designed to show to show the vendor name, based on what the VendorID field value is. But when I first create a ItemVendor record, the VendorID value is null, so it is unable to find a vendor name, and errors.





Re: Windows Forms Data Controls and Databinding combobox giving an error when field is null

Suprotim Agarwal

How are you trying to create the new record

Also make sure to check the ItemVendor table in the db to confirm that it has the correct ID's stored

HTH,
Suprotim Agarwal

-----
http://www.dotnetcurry.com
-----






Re: Windows Forms Data Controls and Databinding combobox giving an error when field is null

fweeee

Suprotim,

I'm trying to create the new record by clicking on the empty row at the bottom (which has an * on it). I havnt actually written any code to insert or save or anything - it errors before I try to do that. The second I click on the empty line, thats when I get the error.

I should point out a few things:
1) I'm kind of new to the Visual Studio development environment, so I doubt this is an advanced issue.
2) If I have manually created records (in SQL SErver Management Studio), it allows me to change the values from one vendor to another just fine.




Re: Windows Forms Data Controls and Databinding combobox giving an error when field is null

Suprotim Agarwal

Hi,

Yes I understand that. However sometimes the most logical of things are the ones which cause problems. So someone really has to try out your code to reproduce the behaviour. Anyways since that is not available, try this link which explains about a similar problem :

http://www.developermania.com/newsgroups/item/76939/Re_DataGridView_Cascading_DataGridViewComboBoxColumn.aspx

HTH,
Suprotim Agarwal

-----
http://www.dotnetcurry.com
-----






Re: Windows Forms Data Controls and Databinding combobox giving an error when field is null

fweeee

No, that didnt help. The example presented there has two comboboxes were the selection in one is dependant on the value of the other - that is not the situation I have. Both combo boxes are independant of each other. In fact, I can disable one of them, and I'll still get the problem with the other. This is just a very simple relationship between two tables where it errors if the foreign key is null or 0 (as neither of these links up with a value in the foreign table).

I'm not doing anything tricky here - it is just my understanding of visual studio/C# which is stopping me from doing this.

(I did try what the link suggested, which was basically doing in code what I was doing with properties, but that gave the exact same result)