Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to remove the "sort arrow" from datagrid column heading? 1

Status
Not open for further replies.

EwS

Programmer
Dec 30, 2002
398
US
I have a sortable grid with 3 columns, so when I click in any of the column headings, an arrow appears indicating the sort order. When I press button X on my panel, I'm reloading the grid with original sort order, so I need to make the "sort arrow" disappear. The following seems to be the only thing that hides the arrow (given i variable keeps track which column has been sorted last), is there anything else that accomplishes the same thing, but takes less time? I'm looking for something that would reset all the columns at once. I tried me.dgvTables.Refresh() and it didn't do the job.

Code:
Me.dgvTables.Columns(i).SortMode = DataGridViewColumnSortMode.NotSortable
Me.dgvTables.Columns(i).SortMode = DataGridViewColumnSortMode.Automatic

Thank you for your help.
 
If you use a DataView as the Grid DataSource you can set the DataView.Sort property to an empty string, which clears the current sort order.

Alternatively, you could look at ways to circumvent the DataGrid sort behaviour and manage it yourself, i.e. pick up the Click event, determine when a column header is clicked and set the sort property/datasource accordingly. I would recommend some investigation along these lines as the default sort behaviour is not ideal.
 
I'm not seting DataSource to anything, this is how I load the grid:
Code:
For Each t As SQLTable In...
  dgvTables.Rows.Add...
Next
It looks like the only solution is to handle sorting by writing my own sort method.
Thanks for the help!
 
You could set the datasource to a dataview and set the .Table property to your DataTable

Code:
dim myDataView as New DataView
myDataView.Table = myTable
myDataView.Sort = ""
myDataGrid.DataSource = myDataView
myDataGrid.Refresh

Personally, I would put this in place as a quick fix and then work on a much better sort method. If this works for you then doing your own sort method will be easier as you just set the DataView.Sort property to whatever you need.
 
techsmith,
Thank you very much for your help! Your code does the trick - it removes the sort arrow from the column heading, and it does it very fast.
One problem that I'm having is how to make my Copy column (see the code below) a DataGridViewCheckBoxColumn and Columns - DataGridViewButtonColumn (the button is supposed to bring up a dialog box that would display all the columns for the selected table). It's easy to define the column type when you're adding columns to the collection for the Columns property of the datagrid, but besides the basic properties, such as ReadOnly, MaxLength, is there a way to set a type for a dataTable column? Or is there a way to link the DataView columns to the DataGrid columns (I got rid of my DataGrid columns collection since I ended up with 8 columns, instead of 4)?
Here's how I'm loading the grid:
Code:
Dim dt As New DataTable
dt.Columns.AddRange(New DataColumn(3) {New DataColumn("Copy"), New DataColumn("Source Table"), New DataColumn("Destination Table"), New DataColumn("Columns")})
...
Dim dr1 As DataRow = dt.NewRow
...
dr1.ItemArray = New Object(2) {checked, table.Name, table.Name} ' NOTE: checked can be either "T" or "F"
...
dt.Rows.Add(dr1)
...
Dim myDataView As DataView
myDataView = New DataView(dt)
myDataView.Sort = ""
dgvTables.DataSource = myDataView
dgvTables.Refresh()
 
Try searching for Threads about DataGridViewCheckBoxColumn and DataGridViewButtonColumn. I'm sure this has come up before, but it's not something I'm familiar with.

Glad to be of help with the first bit.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top