Can't Delete DataGridViewRow WIthout Exception

I have a DataGridView with 3 columns that is bound to a DataTable.  The first DataGridView column is a DataGridViewLinkColumn that is clicked by the user to delete a row.  In my example below, if you left-click the cell that contains text "$200.00",
press the Delete key to clear the data, then left-click the "Remove" link on that same row you get a
NoNullAllowedException:  "Column 'Cost' does not allow nulls.".  I'm assuming this is because
Nothing or
DBNull.Value is pushed to the underlying DataRow field "Cost" which doesn't allow nulls.  If the user clicks "Remove" I need to remove the DataGridViewRow and underlying DataRow.  Any ideas how to get around this issue?
Public Class Form1
Private clmRemove As New DataGridViewLinkColumn
Private clmDescription As New DataGridViewTextBoxColumn
Private clmCost As New DataGridViewTextBoxColumn
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' create underlying datatable
Dim dt As New DataTable
Dim remove As New DataColumn("Remove", GetType(String))
Dim description As New DataColumn("Description", GetType(String))
description.AllowDBNull = False
Dim cost As New DataColumn("Cost", GetType(Decimal))
cost.AllowDBNull = False
dt.Columns.AddRange({remove, description, cost})
dt.Rows.Add("Remove", "desc 1", 100D)
dt.Rows.Add("Remove", "desc 2", 200D)
' create datagridview
With Me.DataGridView1
clmRemove.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
clmRemove.HeaderText = ""
clmRemove.DataPropertyName = remove.ColumnName
clmDescription.HeaderText = "Description"
clmDescription.DataPropertyName = description.ColumnName
clmDescription.DefaultCellStyle.WrapMode = DataGridViewTriState.True
clmCost.HeaderText = "Cost"
clmCost.DataPropertyName = cost.ColumnName
clmCost.DefaultCellStyle.Format = "c"
.Columns.AddRange(clmRemove, clmDescription, clmCost)
.AllowUserToAddRows = False
.AllowUserToDeleteRows = False
.RowHeadersVisible = False
.AutoGenerateColumns = False
.EditMode = DataGridViewEditMode.EditOnEnter
.SelectionMode = DataGridViewSelectionMode.CellSelect
.RowTemplate.Resizable = DataGridViewTriState.False
.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders
.DataSource = dt
End With
End Sub
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
If e.ColumnIndex = Me.clmRemove.Index And e.RowIndex > Me.DataGridView1.TopLeftHeaderCell.RowIndex Then
Me.DataGridView1.Rows.RemoveAt(e.RowIndex)
'CType(CType(Me.DataGridView1.Rows(e.RowIndex).DataBoundItem, DataRowView).Row, dsQuoteData.LineItemCustomItemsRow).Delete()
End If
End Sub
End Class
Ryan

Hello,
The following might help in that it will prevent the null exception when a decimal value is missing
Public Class Form1
Private Sub Form1_Load() Handles MyBase.Load
Dim dt As New DataTable()
dt.Columns.Add("DecimalOne", GetType(Decimal))
dt.Columns.Add("DecimalTwo", GetType(Decimal))
dt.Rows.Add(1.23, 2.22)
dt.Rows.Add(93.23, 90.5)
dt.Rows.Add(9.56, 6.0)
DataGridView1.DataSource = dt
End Sub
Private Sub DataGridView1_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
If DataGridView1.Item(e.ColumnIndex, e.RowIndex).IsInEditMode Then
Dim c As Control = DataGridView1.EditingControl
If DataGridView1.Columns(e.ColumnIndex).Name = "DecimalOne" Then
c.Text = NumberCleaner(c.Text)
End If
End If
End Sub
Function NumberCleaner(ByVal str As String) As String
Return System.Text.RegularExpressions.Regex.Replace(str, "[a-zA-Z\b\s-]", "")
End Function
Private Sub DataGridView1_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles DataGridView1.DataError
If e.ColumnIndex = 1 Then
If e.Exception.Message.Contains("not in a correct format") Then
MessageBox.Show("Only 0-9 and decimals are permitted")
End If
e.Cancel = False
End If
End Sub
End Class
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

Similar Messages

  • After settings update can,t delete email without opening it

    After settings update can,t delete email without opening it

    In your mail list, do you see the word 'edit'? If you choose that then you can select whatever mails you want gone and delete them at once.

  • How can I delete history without deleting cookies on safari yosemite?

    how can I delete history without deleting cookies on safari yosemite?

    actually, if I click on history and go to bottom and delete history, with Yosemite, it will delete cookies and all... I get message: if you delete history, you will delete cookies and other information from web sites.
    to avoid that the procedure is this: History > Show History>Select All>Delete. 
    thanks for trying to help.
    good day.

  • Can I delete account without deleting messages?

    I have old email messages from friends and family that I want to keep in my mail folders, but I no longer use that email account. Can I delete the account without deleting all the historic email associated with it? Or do I have to look at that now grayed account name forever in my sidebar?

    thanks. That is just what I needed. I currently have three accounts, including the one I'm deleting.
    If I understand you right, all I need to do is move all my messages out of the INBOX of the account I'm deleting, and into other folders I created under the ON MY MAC section. Deleted account messages will only be deleted from the INBOX. (well, HELP should have said so, instead it just said ALL mail from that account will be deleted).
    Now, if I SENT mail from the account I delete, that account's mail in the SENT folder be deleted also because it has not been moved into a folder I created in the ON MY MAC section, Correct?

  • I have 2 Iphone 4s's with the same apple id can I delete 1 without it effecting the other

    Hi, Ihave 2 iphone 4s's with the same apple id, can I delete 1 of them without it affecting the other phone?

    You should probably tell us exactly what you are trying to accomplish.  Carolyn's answer is correct as always, but here's a small clarification:
    You could, for example, sync and back up one of the iPhones using iTunes on your computer.  Then you might take one of the iPhones and do Settings > General > Reset > Erase All Content and Settings
    Although that action would cleanse all data off that specific iPhone, the data should remain on your iTunes backup and I believe it would remain on the other iPhone.
    There is a difference between specifically deleting apps, messages, etc. on an iPhone versus resetting and cleansing an iPhone.

  • I have two identical Mail accounts. How can I delete one without losing all the mail in both accounts?

    I have two identical Mail accounts.  Don't know how that happened -- the second one just showed up one day. If I try to delete one account, I get a warning that all mail in the accounts will be permanently lost.  How can I delete one of the duplicate accounts without losing messages in both accounts?

    Download the yahoo email app, if you already deleted and reinstalled the email account and still have issue.
    This is a known issue with Yahoo email account.

  • Can i delete imessages without having my ipad mini

    can i delete my ipad mini imessages without having it with me

    How would you propose deleting content from an iOS device to which you do not have access?

  • How can I delete e mails except one by one

    how can I delete e mail messages except one by one?

    thats mostly like deleting them one by one . sure, its a bit faster that way, but i am lucky i dont have to do this since my email accounts are all well managed.
    to OP. you can still delete them all on your mail provider web client. well, mostly... outook.com/gmail.com/ whatever you use

  • How can I delete history without deleting website data

    HHow can I delete safary history without deleting website data

    Create an application from this Applescript with AppleScript Editor and run it whenever you want to clear just the history:
    tell application "Finder"
      activate
      delete file "History.db" of folder "Safari" of folder "Library" of folder "XXXX" of folder "Users" of startup disk
      empty trash
    end tell
    where XXXX is your Home folder name or user short name.

  • Can I delete Itunes without losing my music

    I am having so much trouble with itunes. I keep getting compatibility errors. Can I delete it and reinstall it? I already tried to repair it.

    I keep getting compatibility errors.
    Could you give us the exact text of what those errors are, and when you are recieving them?
    Can I delete it and reinstall it?
    Yes.
    Go to START > Control Panel. In Add or Remove programs, find the listing "iTunes" and click "Remove".
    If you are simply deleting the program like this, your iTunes library will still be safe and saved on your computer under a folder called "iTunes".
    -Kylene

  • Why are two pages yoked when I prepare a newsletter in Pages?  How can I delete one without the other?

    Why are two pages yoked when I prepare a newletter?  How do I delete one without the other?

    They are all part of one Section. Just as you do not (hopefully) rip pages out at random from books, the pages of a Section are linked by the text that flows from one to the other.
    If you want to move or delete particular page/s Insert a Section Break at the end of the page before.
    Peter

  • I am using some one else's icloud how can I delete it without deleting things on my phone

    My icloud does not work why?

    I can't tell you why your iCloud account isn't working unless you explain what isn't working about it.
    To delete the iCloud account without losing the data, start by saving your photo stream photos to the camera roll.  Next, if you're syncing notes with iCloud, open each note and email it to yourself.  You will have to copy and paste the text from the email into new notes.  Then go to Settings>iCloud, tap Delete Account, provide the password (if using iOS 7) and choose Keep on My iPhone when prompted.  This will keep the iCloud data on your phone, with the exception of photo stream photos (which you saved to your camera roll) and notes (which you emailed to yourself).

  • How can i delete all history, except open windows and tabs?

    hi, i can not find any way to delete all my firefox history, except open windows and tabs.
    i could not find any solution about in the web. i did try almost all settings in privacy settings, nothing works. any ideas how to do that, or maybe suggestions for add ons?

    Hello,
    Have you looked in:
    * History (or [[Image: New Fx Menu]] > History) > Clear Recent History...
    You can choose what you want cleared and the time frame.

  • How can you delete users without losing what their username is tied to.

    Hi,
    I'm trying to delete obsolete users out of the system, but if I do I lose anything their user name is still associated with associated with(ex. purchase orders, material request etc.) Can anyone help?

    >
    JC wrote:
    > Just wondering is that an SAP best practice recommendation ? I have never seen anything in the SAP Security Guides indicating that though.
    > Another aspect is that access to any protected program variants is lost. There is a SAP program that unprotects them though.
    This was through some discussions via OSS.  I've always preferred to keep them myself, but there are always exceptions.  The client decided to ask SAP direct and the final recommendation was not to delete the ID's but just expire them.

  • I have two accounts with the same name. One no longer works. Can I delete it without deleting the folders it shares with the other account? How?

    Each account has the same name (e.g. both are called [email protected]). The newer one was set up by accident during a conversion from Outlook. Both access the same inbox and sent mail. The two accounts show up as separate in Thunderbird and both have their own inboxes and sent mail boxes. The older one has not updated since the newer one was created, and the older one can no longer reach the server. I want to delete the old one, but am worried that it may delete the folders for the new one in the process. All of the relevant messages are still hosted on the ISP's server. Please advise whether I can do a simple "delete account" on the old account and not wipe out the new one at the same time.

    Actually, doing a "delete account" does not delete the folders associated with it. They are still there, but unused. There should not be a problem with doing the "delete account".

Maybe you are looking for

  • I am trying to hook my macbook pro up to my old sony trinitron tv! HELP!

    I have all the right cords... at least I think. I have VGA SVGA to S-Video 3 RCA TV AV Converter Cable Adapter, a s video cable (male), and a Mini DisplayPort to VGA Female Adapter for Mac. I tried plugging all these things in, but none of it worked.

  • Old sent messages from 2 years ago are missing!

    I've been using Apple Mail for years, never reallyneeded to go back for any reason. But I'm now searching for a message I sent back 2 years ago. Although it seems that my sent items only go back to just over a year! I always assumed that would remain

  • Need help on procedure

    Hi, can anyone explain me whether i need to make any changes to my procedure or it is perfectly fine. /*WHENEVER SQLERROR EXIT FAILURE; SET VERIFY OFF; --SET SERVEROUTPUT ON;*/ CREATE OR REPLACE PROCEDURE sample_ord_sync_wrapper AS DECLARE    l_reque

  • Sony Flashtool for Xperia J

    When will the Xperia J be included on the Sony Flashtool. It seems strange that higher spec phones are supported but not the lower end phones. People with lower spec phone are more likely to want custom ROMs more than those with the higher spec phone

  • Cannot download anything from App Store, no error.

    I haven't been able to download anything from the Mac App Store. I've tried a few things to help with troubleshooting, and this is what I've gotten so far: - I cannot download anything when I use my computer - I cannot download anything, even if logg