Rashmi Pandit
Rashmi Pandit

Reputation: 23858

How to prevent sorting of data grid view

I am using a DataGridView on windows form. It displays just two columns. By default when the application is run, if I click on the column headers, the datagridview gets sorted based on that column. However, I want to disable sorting on the grid view completely. I was not able to find a property where I could set sorting = false, or something like that.

Can anyone please tell me how to disable grid view sorting?

Thanks :)

EDIT:

Just figured I could set individual columns as NotSortable (posted answer below). Can it be done at the grid view level, rather than individual columns?

Upvotes: 15

Views: 33680

Answers (7)

tyne
tyne

Reputation: 115

Or you can create your own function

Private Sub NotSortGrid()
        For i = 0 To dgvUtil.Columns.Count - 1
            dgvUtil.Columns.Item(i).SortMode = DataGridViewColumnSortMode.NotSortable
        Next i
End Sub

Upvotes: 0

Casey Hilde
Casey Hilde

Reputation: 1

I use a binding source and a datatable for my datagridview. In order to bypass all the underlying autosorting done by the dataview I have a database id number for each row so I can use the datagridview to make a list of my id numbers (FROM THE SELECTED ROWS) then i can search my datatable for each record and make the change there. I could probably just do the same thing straight to the datagridview itself. to be clear im not actually sure if im bypassing the auto sorting but the method here does not miss any rows due to them moving in an auto sort. This may not be good if your database is very large as it involves searching the entire database for each record.

I have a database of employees and i can select multiple rows and then select a job code from a combobox and click the button to set the jobcode for each row. I might not be needing to suspend the binding source but anyways...

heres my code.

Private Sub SetCodeBtn_Click(sender As Object, e As EventArgs) Handles SetCodeBtn.Click
    If current_db_name = "employees" Then
        Dim db_id_col As Integer = FindDataGridColumn("DB_ID", DataGridView1.Columns)
        Dim job_code_col As Integer = FindDataGridColumn("JOBCODE", DataGridView1.Columns)
        Dim new_job_code As String = JobCodesCBX.Text
        Dim db_id_list As IList(Of String) = New List(Of String)
        For Each row As DataGridViewRow In DataGridView1.SelectedRows
            db_id_list.Add(row.Cells.Item(db_id_col).Value)
        Next
        For Each id As String In db_id_list
            For Each table_row As DataRow In tables_data("employees").Rows
                If table_row(tables_data("employees").Columns.IndexOf("DB_ID")) = id Then
                    table_row(tables_data("employees").Columns.IndexOf("JOBCODE")) = new_job_code
                End If
            Next
        Next
        table_changes_made = True
        RefreshDataGridView(DataGridView1)
        BindingSource1.ResumeBinding()
    Else
        MsgBox("You Must Select Rows In The Employees Database To Set A Job Code.")
    End If
End Sub 

Screenshot of my Program

Upvotes: 0

Marcus Santodonato
Marcus Santodonato

Reputation: 136

Staring with .NET 3.0, the GridView has a property called AllowSorting

Upvotes: -1

Marc Gravell
Marc Gravell

Reputation: 1064104

Sorting is, in part, a feature of the data-source. What is the data source in this case? DataTable, perhaps? One option is simply to use a data-source that doesn't support sorting, which is almost all of them. List<T>, BindingList<T> etc - don't provide sorting.

If you must use DataView, you could (I guess) wrap the view with a custom view that re-implements IBindingList (returning false for SupportsSorting), but simply changing the values per column is a lot easier (to the point where it would be crazy to do anything else...)

Upvotes: 2

Rashmi Pandit
Rashmi Pandit

Reputation: 23858

Okay, found the answer. For each column I need to explicitly specify

this.dgv.Columns[1].SortMode = DataGridViewColumnSortMode.NotSortable;

So I wrote my own function in a Helper class

/// <summary>
/// Sets the sort mode for the data grid view by setting the sort mode of individual columns
/// </summary>
/// <param name="dgv">Data Grid View</param>
/// <param name="sortMode">Sort node of type DataGridViewColumnSortMode</param>
public static void SetGridViewSortState(DataGridView dgv, DataGridViewColumnSortMode sortMode)
{
    foreach (DataGridViewColumn col in dgv.Columns)
        col.SortMode = sortMode;
}

and wherever, I need to make grid views unsortable, I call it like this:

Helper.SetGridViewSortState(this.dgv, DataGridViewColumnSortMode.NotSortable);

Upvotes: 30

ScottE
ScottE

Reputation: 21630

For i = 0 To DataGridView1.Columns.Count - 1
    DataGridView1.Columns.Item(i).SortMode = DataGridViewColumnSortMode.Programmatic
Next i

web gridview has a property AllowSorting which is much easier!

Upvotes: 7

GWLlosa
GWLlosa

Reputation: 24433

You could always handle the column header click and double click events yourself, and do nothing in them.

Upvotes: -1

Related Questions