VB - Fill only specific DataGridView columns from db query


I have the following DataGridView in my form:

| Assignment | Name | ID Number | Location |

I have a database query that will select the "Name" and "ID Number" from a table.
"Assignment" and "Location" will be entered manually.
Later I will have a button to export the data from the DataGridView to another database table.

My question is, how do I tell vb to fill only the "Name" and "ID Number" columns from my query?

I'm using VB 08.

Thank you,

dgViewText = "SELECT Name, ID_Number FROM tbl_emp"
data = New DataTable
adapter = New MySqlDataAdapter(dgViewText, conn)
CommandBuild = New MySqlCommandBuilder(adapter)
dgView.DataSource = data

Open in new window

LVL 13
Who is Participating?
ITHelper80Connect With a Mentor Commented:
Try this. Needs to be ran after you pop the DataTable

Turn off AutoGen Columns on the GridView
Dim Name as BoundField = New BoundField()
Dim ID as BoundField = New BoundField()
Dim Assign as BoundField = New BoundField()
Dim Location as BoundField = New BoundField()
Name.DataFeild = "Name"
Name.HeaderText = "Name"
Name.DataFeild = "ID_Number"
Name.HeaderText = "ID Number"
Assign.HeaderText = "Assignment"
Same for Location

Open in new window

Sorry this
Name.DataFeild = "ID_Number"
Name.HeaderText = "ID Number"
should be
ID.DataFeild = "ID_Number"
ID.HeaderText = "ID Number"
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.