I am using a text file to populate a datagridview which allows the user to add, delete or edit entries all of wich works fine.
The first column has no real meaning except that it is used like a primary key, each entry is given a number which is the row index +1 with a 0 added to the front of numbers 1 to 9 to prevent it sorting as 1, 10, 11, 12.....2, 20, 21...etc.
If an entry is deleted this column is given new numbers, first row is 01 second is 02 etc. This maintains the order in which the entries were added and prevents missing numbers.
I can sort by any column, delete entries, add entries or edit entries and everything works except if the datagridview was sorted by a column other than the numbered column and an entry was deleted the numbers in this column were overwritten in the order that it was sorted at that time, loosing it's original order.
I thought that if I placed a line of code to sort by the numbered column "DGVMapID" when the entry was deleted that this would fix my problem, which it did untill a second entry is deleted. That's when I get the error System.ArgumentException: 'Object must be of type String.'
I also had a message about Enable Just My Code but I can't get that to come back.
In case somebody thinks I'm using the wrong name for the column the name of the column is "DGVMapID" the heading at the top of the column is "Map ID".
This is the first time I have tried sorting using code and can't understand why it works fine when the first entry is deleted but crashes when the second entry is deleted.
Can somebody please tell me where I went wrong or what I am missing?
LockTB only enables or disables, sets the visible or invisable properties for textboxes and buttons.
boolChanged is a variable used to indicate that some entry has been added, edited or deleted causing the Save button to be visable and enabled.
The error happens with this line, comment it out and everything is fine. I did try the other lines that are commented out but had the same result. There are no errors in the error list of Visual Studio 2022. The error has to be in this line of code.
DataGridView1.Sort(DataGridView1.Columns("DGVMapID"), ListSortDirection.Ascending)
Private Sub BtnDelete_Click(sender As Object, e As EventArgs) Handles BtnDelete.Click
'Dim ColumnToSort As DataGridViewColumn
'Dim direction As ListSortDirection
'ColumnToSort = DataGridView1.Columns("DGVMapID")
'direction = ListSortDirection.Ascending
'DataGridView1.Sort(ColumnToSort, direction)
DataGridView1.Sort(DataGridView1.Columns("DGVMapID"), ListSortDirection.Ascending)
DataGridView1.Rows.Remove(DataGridView1.CurrentRow)
'Renumber the Map IDs after delting an entry
For i = 0 To DataGridView1.RowCount - 2
If DataGridView1.Rows(i).Cells(0).Value >= 1 And DataGridView1.Rows(i).Cells(0).Value <= 10 Then
DataGridView1.Rows(i).Cells(0).Value = "0" & DataGridView1.Rows(i).Index + 1
Else
DataGridView1.Rows(i).Cells(0).Value = DataGridView1.Rows(i).Index + 1
End If
Next
'DataGridView1.Refresh()
boolChanged = True
Call LockTB()
End Sub