How do you catch the delete event on a datagrid

Hi All,

I have a datagrid that works. When the user presses the delete button on their keyboard it seems to delete the row from the grid. How do I know what has been deleted from the grid so that I can actually remove it from my actual data. Is there an event I catch or can I compare the data in the grid with my data source.

Please help,

Danny

PS. I am using VB.NET 2003 (C# answers are fine)


Answer this question

How do you catch the delete event on a datagrid

  • Scott Lezberg

    Their is an event that is called when you hit the delete key to delete a selected row called  UserDeletingRow. 

    And then to get the row that being deleted

    int intRowIndex = e.Row.Index;      // Gets index of that row

    string strRowID = dgServers.Rows[intRowIndex].Cells[0].Value.ToString(); //retrieves Id field of the record if you have one to use in deleteing the record from your database.

     

     


  • askman

    Hi,

    Private Sub grdFunctions_UserDeletingRow work very nice...
    I have coded my function as below. But in the message box. No matter i click on Yes or No. It insist to delete the selected row.
    Can you please teach me what is the essential code that i missing. I only want the record to be delete when user pressing Yes. And don't do anything if user press No..

    You can help me... please...
    Thank


    Private Sub dgSupplier_UserDeletingRow(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowCancelEventArgs) Handles dgSupplier.UserDeletingRow
    'prompt a msg when deleting a row
    Dim answer As Integer
    answer = MsgBox("Are you sure that, you want to delete this record ", MsgBoxStyle.YesNo, "PO System")
    If answer = 6 Then
    MsgBox("O")
    Else
    End
    End If
    End Sub



  • imshally81

    Sorry, I left off the "= True" assignment; glad you caught that.

    I'm not sure I understand what you are looking to do as far as numbering the rows. If you want Row1 to have a value 1, Row2 to have a value 2, etc. then you could try something like:

    Form1.DataGridView1.Rows(rowIndex).Cells(0).Value = rowIndex + 1

    (+1 since the row indices are zero-based)

    If that's not what you mean, then please explain your expectations a bit more clearly.


  • Nats_007

    Couldn't you catch the KeyPressed event
  • Ramvarun

    The Data Grid View FAQ here should answer your question -
    http://www.windowsforms.net/Default.aspx tabindex=5&tabid=60


    From the FAQ -
    "When the user selects a row in the DataGridView and hits the delete key, the UserDeletingRow event fires."


  • teuneboon

    Do you use an DataAdapter to fill your grid
  • Killaars

    Check out the SyncFusion DataGrid faq: http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx#q889q< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

     

    -mark

    Program Manager

    Microsoft

    This post is provided "as-is"

     


  • dzimmy

    It doesnt seem to fire. I tried putting a message box in it just to see if my logic was wrong but it didnt fire at all.

  • LeHobbit31

    No, I  am using a bindable list of business objects (I using ideablade).

  • pompeybob44uk

    Try this Works for DataGridViews, not sure about DataGrids.

    Code Snippet

    Dim answer As Integer = MsgBox("Are you sure that, you want to delete this record ", MsgBoxStyle.YesNo, "PO System")
    If answer = vbYes Then
    MsgBox("Deleted.")

    ElseIf answer = vbNo Then

    e.Cancel

    End If


  • zeyansoft

    This seems like it ought to be a realy common problem. Surely someone out there has solved this issue.

  • Vipul Patel - MSFT

    Great! It work...
    I need to add a true value into e.Cancel as below,

    e.Cancel = True

    Beside this, can you please teach me how to write the number (no.) in datagrid first column.
    I do not want to use database record's id. because it's not index from 1 to 10. Sometimes i would be 3,6,1,5,2,etc.

    In this kind of situation, what i think is to write a number in front of each row. Start from 1 until the last record. Do you know how to do that Or you may have any other better solution for it.

    Thanks...


  • How do you catch the delete event on a datagrid