|
-
May 1st, 2007, 10:35 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] [2005] Datagrid
Can anybody point me the correct direction in deleting rows in a datagrid? I've tried searching on msdn and even here on the forum but I still haven't found anything. This is the current code I have:
Code:
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
Dim strRoll2 As String
' strRoll2 holds the Delete Query
strRoll2 = "Delete from Param2 where ParamSetName = @ParamSetName And " _
& "Reservoir = @Reservoir And Description = @Description "
Dim DaRoll As New OleDb.OleDbDataAdapter(strRoll2, gConn)
DaRoll.DeleteCommand = New OleDbCommand(strRoll2, gConn)
Dim rollBack As New OleDbParameter
rollBack = DaRoll.DeleteCommand.Parameters.Add("@ParamSetName", OleDbType.VarChar)
rollBack.SourceColumn = "ParamSetName"
rollBack.SourceVersion = DataRowVersion.Original
rollBack = DaRoll.DeleteCommand.Parameters.Add("@Reservoir", OleDbType.VarChar)
rollBack.SourceColumn = "Reservoir"
rollBack.SourceVersion = DataRowVersion.Current
rollBack = DaRoll.DeleteCommand.Parameters.Add("@Description", OleDbType.VarChar)
rollBack.SourceColumn = "Description"
rollBack.SourceVersion = DataRowVersion.Current
Dim row As DataRowCollection = dsPar.Tables(0).Rows
' If the delete is not successful then the Catch will be executed
Try
DaRoll.Update(dsPar)
Catch x As Exception
' The error will be displayed in a messagebox
MsgBox(x.Message)
End Try
DaRoll.Dispose()
Me.FillDgDelete()
End Sub
Using Visual Studio 2008
Please mark your thread RESOLVED if you no longer need help.
-
May 1st, 2007, 11:17 AM
#2
Frenzied Member
Re: [2005] Datagrid
Hello,
Why don't you get the PK of the currently selected row.
vb Code:
pkRow = mydatagrid.currentRow.cells("PK_Row").value
Then use the PK to delete the row from the database and then fill the datagrid again with updated information from the database.
You might find it easy if you where to use a bindingsource.
Hope that helps,
Steve
-
May 1st, 2007, 11:28 AM
#3
Re: [2005] Datagrid
Agreed.
If you had the data bound from the beginning, you wouldn't even need to make a call to get the PK. Just configure the data source to generate Update,Insert, and Delete statements. Then, in your code where you need to delete the row, you'd just do something like DetailsView1.DeleteItem() or whatever object you have bound to the data source.
-
May 1st, 2007, 11:35 AM
#4
Thread Starter
Fanatic Member
Re: [2005] Datagrid
I do have the data bound while filling the datagrid. That code is here:
Code:
Private Sub FillDgDelete()
Try
dsPar.Clear()
Dim strParm As String
strParm = "Select ParamSetName as Parameter,Reservoir,Description from Param2"
Dim daParm As New OleDb.OleDbDataAdapter(strParm, gConn)
daParm.Fill(dsPar)
dgDelete.DataSource = dsPar.Tables(0)
Catch x As Exception
' The error is displayed in a messagebox.
'MsgBox(x.Message)
MessageBox.Show("There was an error in populating the data. " & x.Message, "ReservoirGrail", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Could I use this to delete a row? Sorry if it sounds like a dumb question, but I've never done this before?
Using Visual Studio 2008
Please mark your thread RESOLVED if you no longer need help.
-
May 1st, 2007, 01:13 PM
#5
Thread Starter
Fanatic Member
Re: [2005] Datagrid
I'm using this instead now:
Code:
Dim dv As New DataView(Me.dsPar.Tables(0))
dv.Delete(0)
My question now is how will i tell it to delete the current row that is highlighted instead of having to give it an index?
Using Visual Studio 2008
Please mark your thread RESOLVED if you no longer need help.
-
May 1st, 2007, 01:17 PM
#6
Re: [2005] Datagrid
You pretty much have to give it the index to do anything. With a bit of fiddling around, you can create a CommandArguement and suck the index out of the object when it's triggered... fun stuff.
-
May 2nd, 2007, 09:49 AM
#7
Thread Starter
Fanatic Member
Re: [2005] Datagrid
Ok, I'm able to delete the row from the datagrid but if I close it and reopen it, it still has the same items that were deleted on the grid. I tried doing the .AcceptChanges() but that didn't work. This is what I have to delete the row:
Code:
Dim dv As New DataView(Me.dsPar.Tables(0))
dv.Delete(dgDelete.CurrentRowIndex())
dsPar.Tables(0).AcceptChanges()
'Me.FillDgDelete()
Me.dgDelete.DataSource = dv
Using Visual Studio 2008
Please mark your thread RESOLVED if you no longer need help.
-
May 2nd, 2007, 03:59 PM
#8
Thread Starter
Fanatic Member
Re: [2005] Datagrid
I'm now trying to use the adapter to delete the rows but it doesn't delete them. I don't get an error, it doesn't do anything. This is what i have:
vb Code:
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
Dim command As OleDbCommand
Dim parameter As OleDbParameter
command = New OleDbCommand("Delete * from Param2 WHERE ParamSetName = ?", gConn)
parameter = command.Parameters.Add("ParamSetName", OleDbType.Char, 10, "Parameter")
parameter.SourceVersion = DataRowVersion.Original
Me.daParm.DeleteCommand = command
End Sub
Anybody?
Using Visual Studio 2008
Please mark your thread RESOLVED if you no longer need help.
-
May 2nd, 2007, 04:00 PM
#9
Re: [2005] Datagrid
When you delete a row in your datatable, the rowstate of that row is marked as RowState.Deleted (but the datarow is still there in the datatable until you call datatable.AcceptChanges). The table in your database has no changes until you update it... So if you call acceptchanges before actually updating your database, all it does is to remove any deleted row and reset rowstate of the reamining rows; The next time when you load the same table from database, those "supposed to have been deleted" rows will show up again...
So NEVER call acceptchanges before updating your database.
-
May 2nd, 2007, 04:04 PM
#10
Thread Starter
Fanatic Member
Re: [2005] Datagrid
stanav-i changed the code. could you please help me with the new code?
Using Visual Studio 2008
Please mark your thread RESOLVED if you no longer need help.
-
May 4th, 2007, 09:28 AM
#11
Thread Starter
Fanatic Member
Using Visual Studio 2008
Please mark your thread RESOLVED if you no longer need help.
-
May 4th, 2007, 10:18 AM
#12
Thread Starter
Fanatic Member
Re: [2005] Datagrid
how can you update your changes to the datagrid? I have been trying to use the adapter.Update() but it tells me that I don't have a valid Delete Command.
Is there any other way of updating without using the adapter?
Using Visual Studio 2008
Please mark your thread RESOLVED if you no longer need help.
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
|