Sunday, June 30, 2013

Making DataGridView Column not Sortable Visual Studio

Click her for Section 1: Making DataGridView Column not sortable

In previous section, I have explained about making DataGridView Column not sortable in programmatic way i.e. using codes.
In this post I will be explaining you to make DataGridView Column not sortable using the column property.

#Please refer this section only if you use Visual Studio IDE or any other IDE that supports drag and drop of controls. Visit Section 1 if you use console/commant prompt to run the GUI.

Requirement: GUI form with DataGridView containing atleast one column

#Here I have used Visual Studio 2010 to design the GUI

Do the following Steps now:

1) Go to tool box in left-side/pane ( default) than choose DataGridView from Data section and drag to form.
Fig: 2.1: Selecting DataGridView
                  #You can resize the DataGridView according to your will ( how big you need)
Fig 2.2: DataGridView added in form
2) To add column in DataGridView: Right Click the DataGridView and select 'Add Columns...'
Fig 2.3: Add Column dialog after clicking 'Add Columns...'
3) Give Name to Column and specify Type and Header Text of Column
    #You can leave the default also. In this tutorial I have used default values

4) Click [Add] button
    #You can add as many columns as you want. Here, I have added three columns.
Fig: 2.4: Columns added
5) Now, Right Click the DataGridView and select 'Edit Columns...'
Fig. 2.5: Edit Column Dialog after clicking 'Edit Columns...'
6) Select Column in Selected Columns in left pane of dialog that you want to make not sortable.

7) Now, search the 'SortMode' property in Unbound Column Properties in left section.

8) Now, Click the combo-box for options available
Fig 2.6: Selecting the NotSortable mode

9) Select 'NotSortable' and click [OK] button

Now, your not sortable column is ready

#You can make all columns of DataGridView not sortable form this method.

If you have any queries than leave in the comment(s).

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).