Results 1 to 9 of 9

Thread: Delete a Datagrid row

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219

    Question Delete a Datagrid row

    Will anyone show me how to delete a datagrid row? I populated the datagrid with a dataset. I want to be able to select the row and then click on the delete button to delete the row. Then I want to update the database according to the changes to the datagrid.

    Many thanks in advance!

    ljCharlie

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    To be able to identify which row was selected, use this :
    VB Code:
    1. Private Sub DataGrid1_CurrentCellChanged(ByVal sender As
    2. Object, ByVal e As System.EventArgs) Handles DataGrid1.CurrentCellChanged
    3.         'This method :
    4.         MsgBox("Row number is " & DataGrid1.CurrentCell.RowNumber)
    5.  
    6.         'Or this method :
    7.         'MsgBox(DataGrid1.CurrentRowIndex())
    8.     End Sub

  3. #3
    Lively Member
    Join Date
    Nov 2002
    Location
    Malaysia
    Posts
    124
    I know your problem and i have the solution, i will post the solution for you on Monday, because my code is in office's pc. Now is weekend.

  4. #4
    Lively Member
    Join Date
    Nov 2002
    Location
    Malaysia
    Posts
    124
    VB Code:
    1. Private Sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDelete.Click
    2.         Dim Response As VariantType
    3.         Response = MessageBox.Show("Confirm Delete Record?", "Delete Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
    4.         If (Response = vbYes) Then
    5.             StatusBar1.Panels(0).Text = "Deleting..."
    6.             If (Me.BindingContext(DataSet11, "DatabaseTable").Count > 0) Then
    7.                 Me.BindingContext(DataSet11, "DatabaseTable").RemoveAt(Me.BindingContext(DataSet11, "DatabaseTable").Position)
    8.                 Me.UpdateRemove()
    9.             End If
    10.         Else
    11.             Return
    12.         End If
    13.     End Sub
    14.  
    15.     Public Sub UpdateRemove()
    16.         'Create a new dataset to hold the changes that have been made to the main dataset.
    17.         Dim objDataSetChanges As ProgramName.DataSet1 = New ProgramName.DataSet1()     'Stop any current edits.
    18.         Me.BindingContext(DataSet11, "DatabaseTable").EndCurrentEdit()
    19.         'Get the changes that have been made to the main dataset.
    20.         objDataSetChanges = CType(DataSet11.GetChanges, ProgramName.DataSet1)
    21.         'Check to see if any changes have been made.
    22.         If (Not (objDataSetChanges) Is Nothing) Then
    23.             Try
    24.                 'There are changes that need to be made, so attempt to update the datasource by
    25.                 'calling the update method and passing the dataset and any parameters.
    26.                 Me.UpdateRemoveSource(objDataSetChanges)
    27.                 DataSet11.Merge(objDataSetChanges)
    28.                 DataSet11.AcceptChanges()
    29.             Catch eUpdate As System.Exception
    30.                 'Add your error handling code here.
    31.                 'Throw eUpdate
    32.                 StatusBar1.Panels(0).Text = "Referential Constraint Enforced"
    33.                 MessageBox.Show(eUpdate.Message)
    34.                 'Me.sorting()
    35.             End Try
    36.             'Add your code to check the returned dataset for any errors that may have been
    37.             'pushed into the row object's error.
    38.         End If
    39.  
    40.     End Sub
    41.     Public Sub UpdateRemoveSource(ByVal ChangedRows As ProgramName.DataSet1)
    42.         Try
    43.             'The data source only needs to be updated if there are changes pending.
    44.             If (Not (ChangedRows) Is Nothing) Then
    45.                 'Open the connection.
    46.                 Me.OleDbConnection1.Open()
    47.                 'Attempt to update the data source.
    48.                 Adapter1.Update(ChangedRows)
    49.             End If
    50.         Catch updateException As System.Exception
    51.             'Add your error handling code here.
    52.             Throw updateException
    53.         Finally
    54.             'Close the connection whether or not the exception was thrown.
    55.             Me.OleDbConnection1.Close()
    56.         End Try
    57.         StatusBar1.Panels(0).Text = "Record Deleted"
    58.         MsgBox("      Record Deleted !       ", MsgBoxStyle.Information)
    59.         cmdDelete.Visible = False
    60.     End Sub


  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219
    Thank you very much for the help. I'm greatly appreciated. I'll give that a try.

    ljCharlie

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219
    I have an error at this line:
    StatusBar1.Panels(0).Text = "Deleting..."
    And the error is:
    An unhandled excepting of type 'System.ArgumentOutOfRangeException' occured in mscorlib.dll

    Additional information: Index was out of range. Must be non-negative and less than the size of the collection.
    And if I comment out StatusBar1.Panels(0).Text = "Deleting...", I'll get this error:

    An unhandled exception of type 'System.ArgumentException' occurred in system.windows.forms.dll

    Additional information: Can't create a child list for field DatabaseTable.
    The above error occured on this line: If (Me.BindingContext(DataSet11, "DatabaseTable").Count > 0) Then

    ljCharlie

  7. #7
    Lively Member
    Join Date
    Nov 2002
    Location
    Malaysia
    Posts
    124
    Yeah.....comment out the statusbar.panels(0).text = "Deleting..." if you don't have any statusbar.

    Me.BindingContext(DataSet11, "DatabaseTable").EndCurrentEdit()

    DatabaseTable is the name of DBMS table like eg. Employee, Supplier, Customer or etc.

    You type in maybe "Employee" Instead of "DatabaseTable". "DatabaseTable" was just to show you what kind of thing we type in. So if the table you want to delete is a "Employee" table, then the code is look like below

    VB Code:
    1. If (Me.BindingContext(DataSet11, "Employee").Count > 0) Then
    OK?

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219
    Thanks for clearing that up. I didn't get any error after I made those changes that you suggested; however, the record did not get deleted. The record was still in the database. But I must tell you, I made three changes to the script you gave me. First, I put the sub routines in a separte module instead of in the main form where the datagrid is located. Then I pass the conrtols byval into this module. Second, instead of Me.UpdateRemove() I have UpdateRemove(DataSetProfile, mainForm, dbConnection, DataAdapterProfile) . Third, instead of Me.UpdateRemoveSource(objDataSetChanges), I have
    Code:
    Try
         If (Not (objDataSetChanges) Is Nothing) Then
             'Open the connection.
              mainDBconnection.Open()
             'Attempt to update the data source.
             DAuserProfile.Update(objDataSetChanges)
         End If
    Catch updateException As System.Exception
             'Add your error handling code here.
              Throw updateException
              Finally
              'Close the connection whether or not the exception was thrown.
              mainDBconnection.Close()
    End Try
               'statusBarMain.Panels(0).Text = "Record Deleted"
                MsgBox("      Record Deleted !       ", MsgBoxStyle.Information)
               'cmdDelete.Visible = False
    I combined the two sub routines to one. I wonder if any of these might have contributed to the problem of not deleting the record in the database file. Like I said, there is no error generated, but the record has not been deleted.

    Many thanks for your help.

    ljCharlie
    Last edited by Chong; Aug 26th, 2003 at 08:12 AM.

  9. #9
    Lively Member
    Join Date
    Nov 2002
    Location
    Malaysia
    Posts
    124
    I have no idea, but the record sure can be deleted if you follow the code that i gave you...



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width