Sunday, June 30, 2013

Making DataGridView Column not sortable .Net

This is the first post of this blog.

In this post I will be explaining and giving you the code for making DataGridView columns non sort-able.

#Note before going down: This method works for any DataGridView either that has columns you added on programmatic way or you added during designing GUI in Visual Studio.

With default the DataGridView columns are sortable i.e. you can sort the items in DataGridView on ascending or descending order of date in the column of DataGridView; if it contains numeric values in a DataGridView than it will give you/user to sort data according to the values ( ascending or descending) just by clicking on header of the column of DataGridView. There will occur some cases in you must disable that property like if you are adding custom data in it ( at last adding a row for Total amount  for demonstrating  bill format and so on...).
Here is the code below:

1)     Making every column of DataGridView non sortable:

        'DataGridView1 is the DataGridView that you had added in your GUI
        For Each column In DataGridView1.Columns 
            column.SortMode = DataGridViewColumnSortMode.NotSortable
        Next

2)     Making only one column non sortable

        '.Columns(1) the index of columnt that you want to make non sortable
        DataGridView1.Columns(1).SortMode = DataGridViewColumnSortMode.NotSortable

This is all about making DataGridView's column non sortable or not sortable. If you have any queries please leave comment(s).

1 comment:

  1. Thank you very much!
    After long searching you save me!

    ReplyDelete