Friday, September 27, 2013

Make DataGridView Height Automatic

In this post I will be showing you a stepwise procedure to make the DataGridView height automatic i.e. increase with the number of rows added.

Context:
a)Given that multiple of rows are required in DataGridView but you are unknown about how many rows are required.

b)You want your DataGridView height increased automatically when new row(s) are/is added.

Context example: You are to provide user to names of his friends in his classroom with their some detail like age, sex, etc. and etc. Now, the problem is you don’t know how many friends he has in his classroom; maybe 10 or 20 or 30 or 31. Let’s suppose, you want to use DataGridView to show all data the user has entered. There is no problem when you add the data in DataGridView by adding rows in it, but the problem arises when your DataGridView height is not sufficient to show all rows in it; you will see vertical scrollbar in the side of DataGridView afterward. You want to show all rows (whatever the number of rows datagridview has) without scrollbar (i.e. see all rows without scrolling down like a table). Here, you can use my concept.

When DataGridView height is not automated:
Fig 1: Form Design View
Now I am adding 10 sample data programmatically (from coding) and running the program.
Fig 2: Form running view where you can clearly see the scrollbars in side of the DataGridView

Fig 3: Form running view. You can see that when DataGridView size is more than the extra space below the table is shown. Note: Size is changed manually in design view
Fig 4: Form running view after the height is made automatic.

Context solution: Increase DataGridView height automatically when new rows are added.

Here goes the solution for this problem:

11)Create a DataGridView and add columns.
22)Create a new Sub (copy paste below)
Private Sub DataGridView1_RowsAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowsAddedEventArgs) Handles DataGridView1.RowsAdded
End Sub
# This Sub handles the event when rows are added in DataGridView1

#Note: You can create this Sub by double clicking the DataGridView and going to coding section of Form and clicking the combo shown below and selecting ‘RowsAdded’. Be careful don’t click anywhere else after double clicking the DataGridView (or jus remain in the sub where double click takes you and then click combo)

Fig 5: Combo location in code view to add the Sub

33)Now, add code below in Sub created in step 2
DataGridView1.Height = DataGridView1.Height + DataGridView1.Rows(DataGridView1.Rows.Count - 1).Height

# It changes the DataGridView height by adding the height of row added.
#Note: this is not sufficient to make dataGridView look like in fig 4.

44)Now, add the below code in form load Sub; just below the declaration of Sub:
DataGridView1.Height = DataGridView1.ColumnHeadersHeight + 24

#like below:

Private Sub test_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DataGridView1.Height = DataGridView1.ColumnHeadersHeight + 24
End Sub

#24 is added because there will be an extra row to add data in the DataGridView (see row with header *) which size is 22px and 2px extra is for the margin of the DataGridView. If ‘add data’ is not enabled in dataGridView than use 2. Check yourself by changing the value.

Fig 6: when new data is being added in DataGridView from Fig 4.
#Note: 10 data are added programmatically.

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

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