5 Replies - 1675 Views - Last Post: 05 March 2013 - 01:11 PM Rate Topic: -----

#1 swt_pallavi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 03-March 13

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..
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


Is This A Good Question/Topic? 0
  • +

Replies To: this is code for deleting a selected row from datagridview..

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

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.
Was This Post Helpful? 0
  • +
  • -

#3 swt_pallavi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 03-March 13

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"?
Was This Post Helpful? 0
  • +
  • -

#4 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

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

This post has been edited by CharlieMay: 05 March 2013 - 12:59 PM

Was This Post Helpful? 0
  • +
  • -

#5 swt_pallavi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 03-March 13

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
"
or i = 1 To DataGridView1.Rows.Count
33
                DataGridView1.Rows(i).Cells("CheckboxColumn").Value = DataGridView1.CurrentCell.Value  'Error is on this line
34
            Next


Was This Post Helpful? 0
  • +
  • -

#6 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

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

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1