How to clear Sorting of a DataGridView?

VBobCat

Well-known member
Joined
Sep 6, 2011
Messages
137
Location
S?o Paulo, Brazil
Programming Experience
3-5
My Form has two data-unbound datagridviews, with the same structure, differing only about which columns are visible.
It aims to allow user to select persons from the first one, and add them to the second one.
In this second datagridview, some settings are to be made about each person, hence it shows some checkbox and combobox columns that remain invisible in the first one.
It works fine, except for a detail. User can sort persons by name in both datagridviews, and that is useful.
But I noticed that if user adds some persons to the second datagridview, then sort it by the name column, and then add some persons more, these enter at the bottom of the list of second datagridview, naturally out of alphabetical order (and that is ok), but still it shows the sort mark on top of the name column, which is misleading.

My main question is this: How to clear the sort mark upon insertion of a new of second datagridviewrow? I've tried MyDataGridView.Sort(Nothing) but it raises an error.

A second, unrelated and maybe simpler question: How can I scroll down the second datagridview that has already more rows than it can show, so that the newly inserted row gets visible just after it is inserted?

Thank you very much!
 
Hi VBobCat,

I do a similar thing. I enabled vertical scroll bars on all my DataGridView objects (From Forms Designer). I do not ever add rows or whatever directly to a DataGridView. Instead I bind a DataTable to each DataGridView and do all the updating on the corresponding DataTable. The only thing I do directly (in software) on the DataGridView is to fetch and set the selected cells and read values from the selected cells. Here is where I add Columns:

VB.NET:
MyDataTable.Columns.Add(ItemArray(i).ToString, GetType(String))

Here is where I add Rows:

VB.NET:
If ItemArray.Length = MyDataTable.Columns.Count Then
    MyDataTable.Rows.Add(ItemArray)
            Else
    Console.WriteLine("There is a problem in the .csv file, the number of records in one")
    Console.WriteLine("data row is not the same as the number of columns defined by the header.")
                Return False
            End If

The above snippets are from a library that handles loading any .csv file to any DataTable, and saving any DataTable to a .csv file. The original version of the library was developed and published by Reed Kimble. I extended and generalized it to handle .csv files of any length with header rows and with a number of security checks built in to intercept user errors in .csv file formats (usually someone hacks the .csv file and omits an essential comma). When I add a row to a DataTable from an array, I always check that the Array.Length is the same as the number of Columns in the data table. Otherwise a system exception is thrown and I don't like those :eek:range:

Hope this helps, if so please give me a good feedback rating?

Best regards,
Rob
 
How to clear the sort mark upon insertion of a new of second datagridviewrow?
One way to do this is to retrieve SortedColumn and reset SortMode to NotSortable, then back to Automatic.
How can I scroll down the second datagridview that has already more rows than it can show, so that the newly inserted row gets visible just after it is inserted?
Set FirstDisplayedScrollingRowIndex.
 
Back
Top