Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All

I am trying to use a bound datagridview to allow users to maintain a simple two column database table. It should be simple right?
The big problem I have is if I place the cursor in the last row which is used for entering a new row and click save without entering anything the whole thing crashes trying to save a blank row.

I want the blank row there so the user can add new data but how do I prevent it from trying to save the blank row?

I have tried to catch it in the RowValidating which if fine if they have entered data but I cannot use remove methods for the blank row as they have not been committed.

The save does an updateall which keeps trying to save the blank row. It fails due to a database constraint but then all other updates fail also.

Anyone found a way around this?

Thanks

What I have tried:

Validating the rows but cannot stop the blank row from adding?
Posted
Updated 31-Mar-21 22:05pm
Comments
CHill60 31-Mar-21 6:13am    
Platform? Language? What is the error thrown?
dnibbo 31-Mar-21 11:08am    
Apologies, I was getting frustrated...
This is .NET 4.5, I am using VB but I am sure c# will be the same as it is not in my code that the problem occurs.
I will post some images to illustrate the problem.
Thanks Chilli

1 solution

How about creating an observable collection from your database and binding to that instead of directly. See c# - What is the use of ObservableCollection in .net?[^] for ideas.

That way you can be quite specific about whether or not you need to insert a new row or update existing
 
Share this answer
 
Comments
dnibbo 1-Apr-21 11:33am    
That looks like a possible solution but I don't understand why we can use a bound datagridview?
In theory you should be able to drag a datatable onto a form and boom, job done!
I need to write a few of these and dont have that much time so was looking for a quick solution. I guess this it is not going to be the case so will write my own as I have in the past.
I am just a bit surprised that there is not an obvious workaround for something that to me seems so simple...

Thanks
CHill60 1-Apr-21 11:40am    
If you don't allow that row at the bottom for users to enter data then it really is that simple. Other approaches I've taken in the past is a "Insert" button which adds a new row for data to be added. Or textboxes (etc) to enter new data (with attendant "Insert" button) which inserts directly to database and then refreshes the DGV. I.e. so the DGV is update/view only.
We've also had the scenario where navigating through the grid would populate the other controls so the DGV became read-only and all updates/inserts via the other controls. We wrote some script to generate those panels.
dnibbo 6-Apr-21 4:23am    
Thanks Chill60
All of your suggestions are good workarounds, I was hoping someone had found a way to get the bound grid to work as a quick win. However, there does not seem to be a way which is a shame :(
I have taken away the bindings and now populate the dgv manually with an 'addable' row. I can then just ignore empty rows. User is delighted with the results so all good now :)

I a sure in the 'old days' you could drag a datasource onto a form and away you go...

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