I am using VB.net and having trouble after sorting a datagridview by a column. System.ArgumentException: 'Object must be of type String.'

Cynolycus 260 Reputation points
2023-02-17T13:29:57.99+00:00

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
Developer technologies VB
{count} votes

Accepted answer
  1. Viorel 122.5K Reputation points
    2023-02-20T01:21:14.1833333+00:00

    Probably you must use ToString:

    DataGridView1.Rows(i).Cells(0).Value = (DataGridView1.Rows(i).Index + 1).ToString()
    

    Maybe you can fix and rewrite the loop:

    For i = 0 To DataGridView1.RowCount - 2
       DataGridView1.Rows(i).Cells(0).Value = (DataGridView1.Rows(i).Index + 1).ToString("00")
    Next
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Reza Aghaei 4,986 Reputation points MVP Volunteer Moderator
    2023-02-20T01:21:32.2133333+00:00

    Use a DataTable or a sortable binding list and sort will be handled automatically.
    Do not store index, unless it's part of the business logic which could not be determined based on other properties. For example positions of football teams in a league is determined based on their total points, so you never need to store their position. Their position is result of ordering based on the points and if you want to show their position in grid, you don't need to have that field in storage.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.