开发者

Best way to disable the column header sorting in DataGridView [duplicate]

开发者 https://www.devze.com 2023-01-28 04:40 出处:网络
This question already has answers here: How to disable sort in DataGridView? (9 answers) Closed 5 years ago.
This question already has answers here: How to disable sort in DataGridView? (9 answers) Closed 5 years ago.

I need to disable the column header s开发者_如何学JAVAorting in DataGridView.

We can do that by setting the property of individual columns like

BalancesGridView.Columns[1].SortMode = DataGridViewColumnSortMode.NotSortable;

If this is the case , then I'll have to loop through all the columns.

Is there a better way ?


No, I think setting the SortMode on the column directly is as good as it gets. But honestly, who cares? What's so bad about a simple loop?


What's so bad about this? If it bothers you to loop over the columns or you have multiple DataGridView's, you can write an extension method for this:

public static class DatatGridViewExtensions
{
    public static void SetColumnSortMode(this DataGridView dataGridView, DataGridViewColumnSortMode sortMode)
    {
        foreach (var column in dataGridView.Columns)
        {
            column.SortMode = sortMode;
        }
    }
}

Use it like this:

BalancesGridView.SetColumnSortMode(DataGridViewColumnSortMode.NotSortable);


I don't know why nobody suggest you Linq way of doing this:

BalancesGridView.Columns.Cast<DataGridViewColumn>().ToList().ForEach(f => f.SortMode = DataGridViewColumnSortMode.NotSortable);


I say looping through columns is not such a great answer, especially if your datasource is changing from time to time. One line of code in the ColumnAdded event does the trick:

e.Column.SortMode = DataGridViewColumnSortMode.NotSortable


well, this is a bit old, but there is a little problem with looping to set the individual column sortmode, for example, you allow user to add more columns, then you have to re-loop it all again, or find the added column and set it's sortmode. that is a bit of more work.

the solution I found is like this link: Disable sorting when clicking DataGridView column header

in it, you just add an event handler of ColumnAdded for that DataGridView, so every time the datagrid adds column, it is automatically set as not sortable

this is actually just like @OldDog's answer, the difference is that in his answer, the sortmode is set in a roundabout way.

Private Sub DataGridView1_ColumnAdded(sender As Object, e As DataGridViewColumnEventArgs) Handles DataGridView1.ColumnAdded
    e.Column.SortMode = DataGridViewColumnSortMode.NotSortable
End Sub


I ran into this problem today. I wrote this method and called it on the form load event.

    public void DisableGridviewSorting(DataGridView grid, int index)
    {
        //Index = DataGridView.Columns.Count
        for (int i = 0; i < index; i++)
        {
            grid.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
        }
    }

It looks like you are pretty much stuck looping through the DataGridView no matter how you do it, unless you use bound data. Then you can set non-sortable for each individual column.


If you build the DataGridView at runtime, you can disable column sorting as the columns are added using the ColumnAdded Event:

private void BalancesGridView_ColumnAdded(object sender, System.Windows.Forms.DataGridViewColumnEventArgs e)
{
     e.Column.SortMode = DataGridViewColumnSortMode.NotSortable;
}


I have done it in this way:

private void gvItReq_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
    for (int colIdx = 0; colIdx < gvItReq.Columns.Count; colIdx++)
        gvItReq.Columns[colIdx].SortMode = DataGridViewColumnSortMode.NotSortable;
}


In VB I use a little subroutine I call for each dgv where I want the columns to be unsortable:

Public Sub subNo_Sort_DGV_Columns(dgv As DataGridView)

  For intColumn_Count As Integer = 1 To dgv.Columns.Count - 1
    dgv.Columns(intColumn_Count).SortMode = _ DataGridViewColumnSortMode.NotSortable
  Next

End Sub


The best way I've found to prevent sorting is to define is to define it a for loop when you think you have to many columns to deal with.

for (int i = 0; i < 10; i++)
{
   dataGridView.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号