|
-
Aug 22nd, 2003, 01:02 PM
#1
Thread Starter
Addicted Member
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
-
Aug 22nd, 2003, 03:01 PM
#2
Sleep mode
To be able to identify which row was selected, use this :
VB Code:
Private Sub DataGrid1_CurrentCellChanged(ByVal sender As
Object, ByVal e As System.EventArgs) Handles DataGrid1.CurrentCellChanged
'This method :
MsgBox("Row number is " & DataGrid1.CurrentCell.RowNumber)
'Or this method :
'MsgBox(DataGrid1.CurrentRowIndex())
End Sub
-
Aug 23rd, 2003, 09:51 AM
#3
Lively Member
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.
-
Aug 24th, 2003, 08:33 PM
#4
Lively Member
VB Code:
Private Sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDelete.Click
Dim Response As VariantType
Response = MessageBox.Show("Confirm Delete Record?", "Delete Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
If (Response = vbYes) Then
StatusBar1.Panels(0).Text = "Deleting..."
If (Me.BindingContext(DataSet11, "DatabaseTable").Count > 0) Then
Me.BindingContext(DataSet11, "DatabaseTable").RemoveAt(Me.BindingContext(DataSet11, "DatabaseTable").Position)
Me.UpdateRemove()
End If
Else
Return
End If
End Sub
Public Sub UpdateRemove()
'Create a new dataset to hold the changes that have been made to the main dataset.
Dim objDataSetChanges As ProgramName.DataSet1 = New ProgramName.DataSet1() 'Stop any current edits.
Me.BindingContext(DataSet11, "DatabaseTable").EndCurrentEdit()
'Get the changes that have been made to the main dataset.
objDataSetChanges = CType(DataSet11.GetChanges, ProgramName.DataSet1)
'Check to see if any changes have been made.
If (Not (objDataSetChanges) Is Nothing) Then
Try
'There are changes that need to be made, so attempt to update the datasource by
'calling the update method and passing the dataset and any parameters.
Me.UpdateRemoveSource(objDataSetChanges)
DataSet11.Merge(objDataSetChanges)
DataSet11.AcceptChanges()
Catch eUpdate As System.Exception
'Add your error handling code here.
'Throw eUpdate
StatusBar1.Panels(0).Text = "Referential Constraint Enforced"
MessageBox.Show(eUpdate.Message)
'Me.sorting()
End Try
'Add your code to check the returned dataset for any errors that may have been
'pushed into the row object's error.
End If
End Sub
Public Sub UpdateRemoveSource(ByVal ChangedRows As ProgramName.DataSet1)
Try
'The data source only needs to be updated if there are changes pending.
If (Not (ChangedRows) Is Nothing) Then
'Open the connection.
Me.OleDbConnection1.Open()
'Attempt to update the data source.
Adapter1.Update(ChangedRows)
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.
Me.OleDbConnection1.Close()
End Try
StatusBar1.Panels(0).Text = "Record Deleted"
MsgBox(" Record Deleted ! ", MsgBoxStyle.Information)
cmdDelete.Visible = False
End Sub
-
Aug 25th, 2003, 08:24 AM
#5
Thread Starter
Addicted Member
Thank you very much for the help. I'm greatly appreciated. I'll give that a try.
ljCharlie
-
Aug 25th, 2003, 09:39 AM
#6
Thread Starter
Addicted Member
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
-
Aug 25th, 2003, 08:51 PM
#7
Lively Member
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:
If (Me.BindingContext(DataSet11, "Employee").Count > 0) Then
OK?
-
Aug 26th, 2003, 08:00 AM
#8
Thread Starter
Addicted Member
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.
-
Aug 26th, 2003, 09:23 PM
#9
Lively Member
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
|