error editing datagridview

dotolee

Member
Joined
Nov 30, 2004
Messages
20
Programming Experience
10+
Hi there. I have a datagridview control that i populate with records from my database. i'm now working on a feature where the user can select multiple records to remove from the datagridview. I do this by setting a certain value to NULL. It works only if the user selects no more than 2 records. The minute more than 2 are selected, i get the following error:
"Index was out of range. Must be non-negative and less than the size of the collection"

Here's the code:
VB.NET:
    Private Sub btnDeleteHHFromTerritory_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeleteHHFromTerritory.Click
        Dim i As Integer = 0
        Dim selectedrowcount As Integer = HouseHolderDataGridView.SelectedRows.Count
        If selectedrowcount > 0 Then
            For i = 0 To selectedrowcount - 1
                HouseHolderDataGridView.SelectedRows.Item(i).Cells.Item(16).Value = System.DBNull.Value
            Next
            MsgBox("TerritoryID reset.  Use Save button to refresh your view")
        Else
            MsgBox("Highlight a row in the Householder list before attempting to delete")
        End If
    End Sub

Any suggestions? ALso, i tried to change the hard coded value of 16 to the actual name of the field but it doesn't recognize it. What am I missing?
Thanks.
 
Hello.

...the actual name of the field but it doesn't recognize it. What am I missing?
Thanks.

That sounds pretty odd...what exact error message does appear?

Btw, you could try to use For Each instead of For

VB.NET:
    Private Sub btnDeleteHHFromTerritory_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeleteHHFromTerritory.Click
        If HouseHolderDataGridView.SelectedRows.Count > 0 Then
            For Each row As DataGridViewRow in HouseHolderDataGridView.SelectedRows
                row.Cells(16).Value = System.DBNull.Value
            Next
            MsgBox("TerritoryID reset.  Use Save button to refresh your view")
        Else
            MsgBox("Highlight a row in the Householder list before attempting to delete")
        End If
    End Sub

Also make sure that the DataGridView SelectionMode is set to FullRowSelect, otherwise you'll have to use SelectedCells().

Bobby
 
that fixed it!

Thank you! i changed the selection mode like you said and used the for each and now everything is working.
what's the difference between the for each and the for i had? the for next loop i had was kinda of working... just want to understand the fix.

also, the error message I'm getting when i try to use the column name instead of a hardcoded index number is:

Column named TerritoryID cannot be found.
Parameter name: columnName

The code looks like:
VB.NET:
            For Each row As DataGridViewRow In HouseHolderDataGridView.SelectedRows
                row.Cells("TerritoryID").Value = System.DBNull.Value
            Next
 
For each autoamtically circling through all the elements, and releases you from the need to check for the end. It's also most likely that the fix was the SelectionMode.

The columnName...is the Name of the column really TerritoryID and NOT the Caption/HeaderText?

Bobby
 
Back
Top