DataGridView creates too many new rows when inserting new data



  • 1. Simple Databinding with Windows Forms
    I am new to programming and am just exploring the way databinding works with Windows forms and am having trouble with some fairly basic customization of data entry. The form uses the SqlDataAdapter and SqlDataset and loads a very simple table, with both text and integer fields. If a textbox is bound to an integer source where the current value is a valid integer, say 9, I can type in the textbox 'rubbish' and move to another textbox. No error is generated, the textbox simply reverts to its previous value of 9. What I would like to happen, is that an error message is shown to the user saying a number is expected. Clicking OK returns the cursor to the textbox - still with the value of 'rubbish' in it so he can see the rubbish he has typed. I would hope this is not a wildly unusual customization to make, however I cannot make it happen. I have tried code in the Binding.Parse event and Textbox.Validating event, and both places allow me to catch the error, but not return focus to the textbox with the offending value showing. I have had a good search through the archives and seen similar posts, but no solution. Does anyone here know one? Thank you for any suggestions
  • 2. KeyPress
    How can I limit the input to just digits 0-9 in VB6 I would use the if KeyAscii<48 or Keyascii >57 then Keyascii=0 -Lou
  • 3. Wie kann ich mein Array() vollst鋘dig in eine Excel Zelle bringen.
    Hi, ich habe ein eindimesionales Array dessen ca. 5 Werte (01234) ich in _eine_ Zelle schreiben will. Um in Excel zu schreiben habe ich folgende Sub geschrieben: Private Sub ExcelAusgabe(ByVal Wert() As Integer) Dim xlApp As Excel.Application Dim xlMappe As Excel.Workbook Dim xlBlatt As Excel.Worksheet Dim xlZelle As Excel.Range Dim intZeilen As Integer Dim EigDat As String 'Pfad zu den eigenene Dateien hier ablegen. EigDat = Environment.GetFolderPath(Environment.SpecialFolder.Personal) xlApp = New Excel.Application xlApp.Visible = True xlMappe = _ xlApp.Workbooks.Open(EigDat & "\MMSBest.xls") xlBlatt = xlMappe.Worksheets(1) xlZelle = xlBlatt.Range("A1") intZeilen = xlZelle.CurrentRegion.Rows.Count xlZelle.Offset(intZeilen, 0).Value = intZeilen xlZelle.Offset(intZeilen, 1).Value = Now() 'Hier liegt das Problem, oder? xlZelle.Offset(intZeilen, 2).Value = Wert xlMappe.Save() xlMappe.Close() xlApp.Quit() End Sub Alles klappt bis auf den Fehler das in der entsprechenden Zelle f "Wert" statt "01234" nur "0" steht. Jemand ne Idee??
  • 4. How could I permanently delete a SQL table during a Store Procedur
    I tried with "Delete tblName" but didnt work. Thanks for any suggestions.
  • 5. Count keystrokes from other applications
    Hi group! I want to make a windows service application that counts all the keystrokes a user does on the computer. Can anyone give me a hint on how i can detect those keystrokes when they are in another application then the counting application itself? __ Thanks in advance Tor Inge

DataGridView creates too many new rows when inserting new data

Postby JT » Thu, 10 Sep 2009 03:14:27 GMT


I am using .NET 2.0 framework on this Winforms project.  I am allowing
users to add new data to a DataGridView and subsequently updating the
database after they are through editing.  My problem is that with a
repeatable sequence of clicks, the DGV will add too many new rows.
Because I'm defaulting the data in some columns, I don't allow editing
them.  However, the data is not inserted in these extra rows.  When I
try to delete them I get an exception.  I can deal with exceptions,
but I want to figure out why it adds too many rows.  Can anyone help
me determine why this is happening or where to trap it?

Because this is hard to describe or visualize, I've put a video at

This has gotten me in other forms and it would be nice to figure it


Re: DataGridView creates too many new rows when inserting new data

Postby Rich P » Thu, 10 Sep 2009 07:14:45 GMT

If you want the contents of your server table to match the content being
displayed in your datagridview - you should use an datatable as
the datasource for your datagridview.  Use an DataAdapter to
pull data from the server table into your local datatable (fill
the local table). When you perform Updtes (edits), inserts, deletes --
use the datagridview events to peform actions on the underlying
datatable.  Then use the ado.Net dataAdapter to transfer these actions
to your server table.  This way your server table will be synchronized
with you local datatable/datagridview.


*** Sent via Developersdex  http://www.**--****.com/  ***

Re: DataGridView creates too many new rows when inserting new data

Postby Rich P » Fri, 11 Sep 2009 03:15:36 GMT

quick note on updating the datagridview underlying datasource table and
the server table:  you can update the underlying local table and choose
when to update the server table.  You can perform
inserts/updates/deletes on the local table.  Then only time the server
table will get updated is when you call dataAdapter1.Update(yourDataset,
"yourlocalTable").  You could have a button for that.  In the meantime,
you can examine your local table from within the datagridview.  If you
have 10 new records in the local table and have peformed updates on 15
rows in the local table and say - deleted 5 rows -- when you call
dataAdapter.update(...) that action will insert only the 10 new rows,
delete only the 5 deleted rows, and update only the rows that had been
updated locally.

If you physically/manually added one row to your local table through the
datagridview - but 3 rows got added (locally) - you should be able to
see this and then debug why this is happening.


*** Sent via Developersdex  http://www.**--****.com/  ***

Re: DataGridView creates too many new rows when inserting new data

Postby JT » Fri, 11 Sep 2009 04:31:36 GMT

Because my reply to you didn't get posted on the site, I'll fill in
some details.  I've determined that the DataGridView.UserAddedRow
event is getting called when this anomaly occurs.  I checked the
DataGridView.RowCount and compared it to the count just prior to
making an edit.  The framework (I assume) is adding 2 rows instead of
just 1.  I can detect it, but removing the extra rows is very
unreliable and other bad behavior begins to happen.  I have just
decided that it is too unreliable to add rows directly in the DGV
(unless I do what Rich said), but even still the user experience would
be very bad.  I am able to modify rows very reliably so I have added
code and controls to add new data and am abandoning adding new rows in
the DGV.  I've never tried it in .NET 3.x, but I doubt anything has
changed.  Reliable is better than fancy.

Re: DataGridView creates too many new rows when inserting new data

Postby Rich P » Fri, 11 Sep 2009 06:28:39 GMT

For whatever it is worth - I have been using
VB4/VB5/VB6/VS2002/VS2003/VS2005/VS2008 very successfully since they all
came out.  Each version either overcomes issues with the previous
version or adds functionality (like Linq in VS2008).  It is all a matter
of how much time you are willing to invest learning how to use the
features.  For good data exchange between sql server and a datagridview
you need to base the datasource of the datagridview on a local
datatable.  My user's user experience is such that I am still gainfully
employed doing what I have been doing for several years.  There is just
no way I could possibly achieve the same functionality with VB6 that I
have achieved with VS2005/VS2008.  The addition of OOP to the .Net
versions of VB has added unbelievable amount of
horsepower/versatility/robustness to the platform.  Just take the time
to learn how to use it correctly.


*** Sent via Developersdex  http://www.**--****.com/  ***

Re: DataGridView creates too many new rows when inserting new data

Postby JT » Thu, 17 Sep 2009 01:02:48 GMT

I appreciate the advice.  I have been doing .NET development
since .NET 1.1 and VS 2003, through .NET 3.5 and VS 2008 (also
gainfully employed).  Tying the DataGridView to a dataset through
binding, such that all modifications are immediate is too restrictive
for my application.  ADO.NET is a staple and I use best practices for
my data access.  I was attempting to provide some unusual
functionality to the user without compromising their desires.  Mostly
I was able to accomplish that but it isn't a good time investment to
pursue that any further.  Maybe I will discover that later versions
have resolved this issue.

Thanks again!

Similar Threads:

1.DatagridView: Data enter in new row go away !

.AllowUserToAddRows = True

.AllowUserToDeleteRows = True

For existing row, cell value edited by user stay until the grid refreshed.

However, the whole row disappears as soon as I move the cursor to previous 
row .

This prevents me to save/update all rows back to the database.

Any help is greatly appreciated.


2.DataGridView leaves an empty row after escape pressed on new row

Return to VB.NET


Who is online

Users browsing this forum: No registered users and 96 guest