Imports System.Data
Imports System.Data.OracleClient
Public Class demoForm4
Private Sub demoForm4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.CheckboxColumn.HeaderText = "Tick to Delete"
Me.DataGridView1.Columns.Insert(0, CheckboxColumn)
AddHandler DataGridView1.CurrentCellDirtyStateChanged, AddressOf dataGridView1_CurrentCellDirtyStateChanged
AddHandler DataGridView1.CellValueChanged, AddressOf dataGridView1_CellValueChanged
For Each itm As DataGridViewRow In DataGridView1.Rows
If itm.Cells(0).Value = True Then
delete.Enabled = True
End If
Next
End Sub
Private Sub dataGridView1_CurrentCellDirtyStateChanged(ByVal sender As Object, ByVal e As EventArgs)
If DataGridView1.IsCurrentCellDirty Then
DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
End If
End Sub
Private Sub dataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs)
If DataGridView1.Columns(e.ColumnIndex).Name = "CheckboxColumn" And e.RowIndex = 1 Then
Dim i As Int32
For i = 1 To DataGridView1.Rows.Count
DataGridView1.Rows(i).Cells("CheckboxColumn").Value = DataGridView1.CurrentCell.Value 'Error is on this line
Next
End If
End Sub
Private Sub Delete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles delete.Click
Try
Dim myOracleConnection As New OracleConnection("User Id=system;Password=root")
myOracleConnection.Open()
Dim Delete = MessageBox.Show("Are You Sure to Delete", "Query", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
For Each itm As DataGridViewRow In DataGridView1.Rows
If itm.Cells(0).Value = True Then
If (Delete = vbYes) Then
Dim sSQL As String = "delete from demo1 WHERE sid= :id"
Dim cmd As New OracleCommand(sSQL, myOracleConnection)
cmd.Parameters.AddWithValue(":id", itm.Cells(1).Value.ToString)
cmd.ExecuteNonQuery()
End If
End If
Next
MessageBox.Show("Record Deleted successfully!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
End Class
5 Replies - 224 Views - Last Post: 05 March 2013 - 01:11 PM
#1
this is code for deleting a selected row from datagridview..
Posted 05 March 2013 - 12:38 PM
i get an error like array index out of bound for the following code..please reply soon..
Replies To: this is code for deleting a selected row from datagridview..
#2
Re: this is code for deleting a selected row from datagridview..
Posted 05 March 2013 - 12:44 PM
Do you have AllowUserToAddRows property set to True?
If so, try setting it to false. A For...Each will attempt to access that row and there is nothing there for it to reference (as in itm.Cells(0))
Just a thought.
If so, try setting it to false. A For...Each will attempt to access that row and there is nothing there for it to reference (as in itm.Cells(0))
Just a thought.
#3
Re: this is code for deleting a selected row from datagridview..
Posted 05 March 2013 - 12:49 PM
i hav set it to false...
one more query "how to make the astrik column of datagridview as the checkBoxColumn"?
one more query "how to make the astrik column of datagridview as the checkBoxColumn"?
#4
Re: this is code for deleting a selected row from datagridview..
Posted 05 March 2013 - 12:54 PM
Well, you could set the rowheadervisible to false which would put the checkbox seemingly the first column. Then I think you could change the color of the checkbox column to the control color to make it appear as the row header with a checkbox. I'd have to play around with it myself.
Also, what line is the error occurring on?
You might have to remark out the Try line and the Catch...End Try block to have the error hilight the line since you're catching it and displaying the error message.
EDIT:
Actually since you're using ex.ToString the line number should appear in the messagebox
Also, what line is the error occurring on?
You might have to remark out the Try line and the Catch...End Try block to have the error hilight the line since you're catching it and displaying the error message.
EDIT:
Actually since you're using ex.ToString the line number should appear in the messagebox
This post has been edited by CharlieMay: 05 March 2013 - 12:59 PM
#5
Re: this is code for deleting a selected row from datagridview..
Posted 05 March 2013 - 01:02 PM
still getting the same error i.e "Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index"
Parameter name: index"
or i = 1 To DataGridView1.Rows.Count
33
DataGridView1.Rows(i).Cells("CheckboxColumn").Value = DataGridView1.CurrentCell.Value 'Error is on this line
34
Next
#6
Re: this is code for deleting a selected row from datagridview..
Posted 05 March 2013 - 01:11 PM
OK
first datagridrows start at index 0
In knowing that let's look at what DataGridView1.Rows.Count is:
SO let's say I have 5 rows
So even though I have 5 rows. My largest index is 4 because we started with 0
So to loop through all the rows in the datagridview our loop would have to start at 0 and stop at 4
And since .Count would result in 5 you would need to subtract 1 from that result
So
For i = 0 to DataGridView1.Rows.Count -1 would result in a loop that runs from 0 to 4.
first datagridrows start at index 0
In knowing that let's look at what DataGridView1.Rows.Count is:
SO let's say I have 5 rows
row1 index 0 row2 index 1 row3 index 2 row4 index 3 row5 index 4
So even though I have 5 rows. My largest index is 4 because we started with 0
So to loop through all the rows in the datagridview our loop would have to start at 0 and stop at 4
And since .Count would result in 5 you would need to subtract 1 from that result
So
For i = 0 to DataGridView1.Rows.Count -1 would result in a loop that runs from 0 to 4.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|