|
-
Oct 10th, 2008, 05:18 PM
#1
Thread Starter
Hyperactive Member
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:
Public Sub LoadDataForStudents()
With frmStudents
EnableAllOnStudents()
.btnNewStudent.Enabled = False
.btnCancelStudent.Enabled = False
.btnResetStudent.Enabled = False
.tsbAccept.Enabled = True
.tsbDelete.Enabled = True
Connection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DatabaseName)
Adapter.SelectCommand = New OleDbCommand("SELECT * FROM Students")
Adapter.SelectCommand.Connection = Connection
Connection.Open()
DS = New DataSet()
Adapter.Fill(DS)
Dim DRow As DataRow
DRow = DS.Tables(0).Rows(.RIndex)
.txtFirstName.Text = DRow.Item("FirstName").ToString
.txtLastName.Text = DRow.Item("LastName").ToString
If DRow.Item("Gender").Equals("Male") Then
.rbMale.Checked = True
ElseIf DRow.Item("Gender").Equals("Female") Then
.rbFemale.Checked = True
End If
.dtpBirth.Value = Date.Parse(DRow.Item("BirthDate").ToString)
.txtPhone.Text = DRow.Item("PhoneNum").ToString
.txtAddress.Text = DRow.Item("Address").ToString
.txtEmail.Text = DRow.Item("Email").ToString
Connection.Close()
End With
End Sub
Public Sub DeleteStudent()
With frmStudents
Connection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DatabaseName)
Adapter.DeleteCommand = New OleDbCommand("DELETE FROM Students WHERE CIndex = @CIndex")
Adapter.DeleteCommand.Connection = Connection
Connection.Open()
Adapter.DeleteCommand.Parameters.Add("@CIndex", OleDbType.Integer, 5, "CIndex")
Dim DRow As DataRow
DRow = DS.Tables(0).Rows(.RIndex)
DRow.Delete()
Adapter.Update(DS)
DS.AcceptChanges()
.dgvStudents.Rows.RemoveAt(.RIndex)
DisableAllOnStudents()
ResetTextOnStudents()
Connection.Close()
End With
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|