r/visualbasic • • Apr 15 '20

Visual Basic and DataTables Guide

Hello!

I have struggled with data tables, how to use them, and understand all the methods for them. If I can't find code close enough to what I want to do, then I can't use them. I was wondering if there was a video tutorial series that does an in-depth explanation of data tables and how to use them to different things properly..? I want to learn to think for myself when utilizing them and not just find other people's code.

6 Upvotes

3 comments sorted by

2

u/andrewsmd87 Web Specialist Apr 15 '20

If you can visualize a data table as an excel file that might help. You have column headers, and then rows of data that fill all of those columns.

There are a lot of functions out there but I'd say the one I use the most is someTable.Select("col1 = true") where col1 = true is like a WHERE clause. It allows you to do sql style filtering on the data in the data table.

1

u/Bonejob VB Guru Apr 15 '20

The best description I can find is;

https://www.dotnetperls.com/datatable-vbnet

In reality a datatable is a collection of datacolumn and datarow objects.

You start by defining you columns. Each column has a single DataType

Dim workTable As DataTable = New DataTable("Customers")
Dim workCol As DataColumn = workTable.Columns.Add("CustID", GetType(Int32))
workCol.AllowDBNull = False
workCol.Unique = True
workTable.Columns.Add("CustLName", GetType(String))
workTable.Columns.Add("CustFName", GetType(String))
workTable.Columns.Add("Purchases", GetType(Double))

This defines the structure of information you will be placing in the columns.

The you add the rows by creating a new row and populating it.

Dim row As DataRow
row = table.NewRow()
row("fName") = "John"
row("lName") = "Smith"
table.Rows.Add(row)

Hope this helps;