How to Calculate Columns in a MS DataGrid

Written by jaime avelar
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Calculate Columns in a MS DataGrid
Use columns to calcualte results in a new column. (binary numbers image by Photosani from

Knowing how to calculate columns in a Data Grid View control using Microsoft Visual Basic.NET, or VB.NET, can save you time when you need to calculate values and present them to the user. A calculated column is the results of a calculation using values from one column or multiple columns. A Data Grid View control displays data in a tabular format. Often the control is used to display data from a database table. In a few steps, you can create a VB.NET project using a Grid with data from a Microsoft Office Access database.

Skill level:

Other People Are Reading

Things you need

  • Microsoft Visual Basic Express
  • Northwind database

Show MoreHide


  1. 1

    Start Microsoft Visual Basic Express, click "New Project" on the left pane of your screen and then select "Windows Forms Application." Click "OK."

  2. 2

    Double-click "Button" on the Toolbox pane to add a new button control. Double-click "DataGridView" to add a new grid control. Double-click "Button1" to open the Form1.vb module.

    Type the following in the first line of your module:

    Imports System.Data.OleDb

  3. 3

    Type the following below the "Button1_Click" procedure to declare your variables:

        Dim oleC As OleDbConnection
        Dim sSQL As String
        Dim data Set As DataSet
        Dim newClmn As DataColumn
  4. 4

    Type the following to query the Orders table from the Northwind Access database:

        sSQL = "SELECT Orders.* FROM Orders;"
        oleC = New OleDbConnection _
        ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Northwind 2007.accdb;")

    Edit the following line of code and type the path and name of your Access database:

    Source=C:\Northwind 2007.accdb

  5. 5

    Type the following to make and open the connection:

        Dim newOleConn As OleDbConnection = New OleDbConnection
        Dim da As OleDbDataAdapter
        da = New OleDbDataAdapter(sSQL, oleC)
        data Set = New DataSet
  6. 6

    Type the following to fill the data set:

        da.Fill(data Set, "Orders")
  7. 7

    Type the following to create the new calculated column and add it the Data Grid View control:

        newClmn = New DataColumn("TotalCost", GetType(Double))
        newClmn.Expression = "[Shipping Fee] * 2"
        Me.DataGridView1.DataSource = dataSet.Tables("Orders")
  8. 8

    Press "F5" to run your program. Click "Button1" to fill the Data Grid View control.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.