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
Start Microsoft Visual Basic Express, click "New Project" on the left pane of your screen and then select "Windows Forms Application." Click "OK."
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:
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
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:
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
Type the following to fill the data set:
da.Fill(data Set, "Orders")
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" dataSet.Tables("Orders").Columns.Add(newClmn) Me.DataGridView1.DataSource = dataSet.Tables("Orders") newOleConn.Close()
Press "F5" to run your program. Click "Button1" to fill the Data Grid View control.
- 20 of the funniest online reviews ever
- 14 Biggest lies people tell in online dating sites
- Hilarious things Google thinks you're trying to search for