Where "Out of Range" on Datagrideview SelectedColumns ?
Did some research and found out how to programmetically sort the DGV. Setup 2 radio buttons to test it out with the following code, when I ran, I got an err msg pointing at "SelectedColumns(1)"
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name:index
Can someone please tell me where is it "Out of range" ? I'm trying to sort based on column 1 data.
----VB Code
Private Sub Sort_Ascending(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbAsc.CheckedChanged
If rbAsc.Checked Then
Dim newColumn As DataGridViewColumn
newColumn = Me.dgvMaterial_Inventory.SelectedColumns(1)
Me.dgvMaterial_Inventory.Sort(newColumn, System.ComponentModel.ListSortDirection.Ascending)
End If
End Sub
Private Sub Sort_Descending(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbDesc.CheckedChanged
If rbDesc.Checked Then
Dim newColumn As DataGridViewColumn
newColumn = Me.dgvMaterial_Inventory.SelectedColumns(1)
Me.dgvMaterial_Inventory.Sort(newColumn, System.ComponentModel.ListSortDirection.Descending)
End If
End Sub
1 Attachment(s)
Re: Where "Out of Range" on Datagrideview SelectedColumns ?
Guys, please put your code in “VBCode” tags. It will be more readable! In case if you don’t know how to do that:
Highlight the code and click the “VBCode” menu item.
Re: Where "Out of Range" on Datagrideview SelectedColumns ?
SelectedColumns is a collection containing all the columns that are selected. If only one column is selected then the collection will only contain one item, thus the range of valid indexes is from zero to zero. If you specify an index that is not within that range then it is out of range, hence the error message. If you select two columns then the collection will contain two items, thus the range of valid indexes will be zero to 1. In that case the index 1 is not out of range.