Click here to Skip to main content
15,791,520 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
datagridview dgv is populated from datatable dt which is populated from a table in the database.
The database table has fields such as:

CustID int not null
fieldNo int not null
field3 varchar(10) null,

On the dgv, I can add a new record as it allows adding a new row.

The field FieldNo is hidden on the dgv. When adding a new row, obviously the user is not adding a fieldNo. As a result, the dgv gives an error that the field does not allow null.
What is the way to solve this please?
Note that it has to have an empty value because once the user has finished adding a new row, then the database stored proc will take care of choosing the correct fieldNo for the table during the insert.

Any thoughts please?

arkiboys 22-Sep-11 9:34am    
FieldNo is a field in the database table which does not allow null.
The datagridview is populated from this table. The datagridview has a hidden field for FieldNo. As I am adding rows into the datagridview by typing values inside each row cell of the grid, then it says the field fieldNo does not allow null. Can I place a dummy value even though the field is hidden while I am inserting new rows to the grid?

Why did you bind the grid to include that field? Don't. If the user can't see the ID field for any reason, don't include it in the bindings. Setup your DGV in code to create your columns instead of letting the DGV automatically create them. Just don't bind to the ID column.

The better solution is to not let users add data directly in a DGV. I always supply a "create new record" form that handles this. It's much easier to control the data entry process and validate the data before adding it to the database.
Share this answer
arkiboys 22-Sep-11 9:43am    
The FieldNo is required so that when an existing row is edited, then the fieldNo field is one of the fields used to update the database table accordingly.
Dave Kreskowiak 22-Sep-11 11:03am    
Yeah, and that information is still in the dataset. You don't need to show it to the user. The DGV is there to show only selected information to the user, not every piece of information in the datasource.

You update the database from the dataset, not from the DGV. You use the DGV to display/edit(Yuk!) only selected information in the data. Updating the database is done from the dataset.
Mehdi Gholam 22-Sep-11 11:23am    
my 5!
then why don't you set the column filedNo to allow null(s).
Share this answer
arkiboys 22-Sep-11 12:48pm    
Because it is not in the debatable and so not in datahridview. This is like a generated I'd value based on existing values... if a user wants to add a new row then he does not know it and so can not be shown on datagridview.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900