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
Re: Confusing Problem - Data Grid View Problems.
... Anyone? Need some Help
Re: Confusing Problem - Deleting Access DB Row & DGV Row - Error! URGENT
Even to me, that code looks confusing and I know a bit about using a DGV (thanks to jmc).
I'm not sure why it looks so complex, because when you delete a row, and update it, you don't need to declare what's in the actual row, besides the first value (and Limit 1). Your database appears to store things different then how you want it displayed(?).
Either way, this looks very confusing...I'd wait for someone else to get this, because some of it looks to be unnecessary (IMHO).