Results 1 to 3 of 3

Thread: Confusing Problem - Deleting Access DB Row & DGV Row - Error! URGENT

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Confusing Problem - Deleting Access DB Row & DGV Row - Error! URGENT

    Alright, I don't know how to explain this so I will do the best I can.

    I have a DataGridView called (DGVStudents).

    I have 2 Subs (LoadDataForStudents() & DeleteStudent())

    Now. here comes my Problem: When I delete a Row & the Data through the .MDB File it goes all fine, but then if I try to click on the Header to Alphabetically Sort it (A-Z or Z-A), my LoadDataForStudents() is loading for the Selected Index of the DGV Row instead of the Hidden Value (CIndex), now If I change my code around to get the CIndex, I run in to some issues, I don't know if that's due to my poor programming or something wrong. Here is my code: Right now its for Getting the CIndex - everything works fine except for the Alphabetically Sorting.

    You will see .RINDEX which Equals e.RowIndex which gets called through a DGV Row Change.

    vb Code:
    1. Public Sub LoadDataForStudents()
    2.         With frmStudents
    3.             EnableAllOnStudents()
    4.             .btnNewStudent.Enabled = False
    5.             .btnCancelStudent.Enabled = False
    6.             .btnResetStudent.Enabled = False
    7.             .tsbAccept.Enabled = True
    8.             .tsbDelete.Enabled = True
    9.             Connection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DatabaseName)
    10.             Adapter.SelectCommand = New OleDbCommand("SELECT * FROM Students")
    11.             Adapter.SelectCommand.Connection = Connection
    12.             Connection.Open()
    13.             DS = New DataSet()
    14.             Adapter.Fill(DS)
    15.             Dim DRow As DataRow
    16.             DRow = DS.Tables(0).Rows(.RIndex)
    17.             .txtFirstName.Text = DRow.Item("FirstName").ToString
    18.             .txtLastName.Text = DRow.Item("LastName").ToString
    19.             If DRow.Item("Gender").Equals("Male") Then
    20.                 .rbMale.Checked = True
    21.             ElseIf DRow.Item("Gender").Equals("Female") Then
    22.                 .rbFemale.Checked = True
    23.             End If
    24.             .dtpBirth.Value = Date.Parse(DRow.Item("BirthDate").ToString)
    25.             .txtPhone.Text = DRow.Item("PhoneNum").ToString
    26.             .txtAddress.Text = DRow.Item("Address").ToString
    27.             .txtEmail.Text = DRow.Item("Email").ToString
    28.             Connection.Close()
    29.         End With
    30.     End Sub
    31.  
    32.     Public Sub DeleteStudent()
    33.         With frmStudents
    34.             Connection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DatabaseName)
    35.             Adapter.DeleteCommand = New OleDbCommand("DELETE FROM Students WHERE CIndex = @CIndex")
    36.             Adapter.DeleteCommand.Connection = Connection
    37.             Connection.Open()
    38.             Adapter.DeleteCommand.Parameters.Add("@CIndex", OleDbType.Integer, 5, "CIndex")
    39.             Dim DRow As DataRow
    40.             DRow = DS.Tables(0).Rows(.RIndex)
    41.             DRow.Delete()
    42.             Adapter.Update(DS)
    43.             DS.AcceptChanges()
    44.             .dgvStudents.Rows.RemoveAt(.RIndex)
    45.             DisableAllOnStudents()
    46.             ResetTextOnStudents()
    47.             Connection.Close()
    48.         End With
    49.     End Sub
    Last edited by Wesley008; Oct 11th, 2008 at 10:54 AM.

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