Solved

DataGridView Delete Problem

Posted on 2011-03-08
2
846 Views
Last Modified: 2012-05-11
I have a Datagridview control in a winForm application bound to a data adapter that references an Access 2007 table. The properties AlowUserToAddRows, AllowUserToDeleteRows, and MultiSelect are all set to True.  Everything works find except the following case:

If the user selects all the rows in the datagridview including the “*” row at the bottom of the datagridview and press the Delete key.  All the displayed records disappear and the “*” row is highlighted (selected).  Next the user clicks in the first column (or any column) of the “*” row and attempts to enter new data.  Upon depressing a keyboard key the message below appears.

    The following exception occurred in the DataGridView:

    System.IndexOutOfRangeException: Index 0 does not have a value. at
    System.Windows.Forms.CurrencyManager.get_Item(Int32 index) at
    System.Windows.Forms.DataGridView.DataGridViewDataConnection.GetError(int32 rowIndex)

    To replace this default dialog please handle the DataError event.

Note: Selecting all the rows except the “*” row at the bottom of the DataGridView works as expected.  Also, selecting only the “*” row while there are other rows in the DatGridView, and pressing the delete key and entering new data works just fine.  The error happens when all rows included the “*” rows are deleted and new data is entered.

Any ideas on how to fix this problem?
0
Comment
Question by:robtrue
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 1

Accepted Solution

by:
rayraymp earned 500 total points
ID: 35088167
If I am correct, by "deleting" the "*" row, you would be throwing off the index of the table.  Try something like the following as a handle for something like "selectionChanged" or "multipleSelected" or an event such as that to lock the "*" cells (it can be modified to do a whole row):

If e.RowIndex < rowcount Then
DataGridView1.Item(e.ColumnIndex, e.RowIndex).ReadOnly = True
End If
0
 

Author Comment

by:robtrue
ID: 35094908
THANKS A MILLION!!!!  The “SelectionChanged” event did the trick.  Below is my code.  
 
With DataGridView1
        ' If all rows including the "*" row (for new record) are selected,
        ' then unselect the last row to prevent index out of range error.
        If .SelectedRows.Count = .RowCount Then .Rows(.RowCount - 1).Selected = False
End With
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

687 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question