- 1). Start Microsoft Visual Basic Express. Click "New Project..." on the left pane of your screen, then select "Windows Forms Application." Click "OK."
- 2). Double-click "Button" on the "Toolbox" pane to add a new button control. Double-click "Button" again to add a second button to your Form. Double-click "DataGridView" to add a new grid control.
- 3). Double-click "Button1" to open the "Form1.vb" module. Copy and paste the following code inside the "Button1_Click" subroutine to define data that will be added to the Data Grid View control:
Dim row0 As String() = {"Ana", "Lopez", "Teacher"}
Dim row1 As String() = {"Sylvia", "Gonzalez", "Business Admin"}
Dim row2 As String() = {"Jaime", "Melendrez", "RF Engineer"}
Dim row3 As String() = {"Mary", "Smith", "Manager"}
With Me.DataGridView1.Rows
.Add(row0)
.Add(row1)
.Add(row2)
.Add(row3)
End With - 4). Type the following to create the "DataGridViewSetup" subroutine that will add formatting and field headers to the Data Grid View control:
Private Sub DataGridViewSetup()
Me.Controls.Add(DataGridView1)
DataGridView1.ColumnCount = 3
With DataGridView1
.RowHeadersVisible = False
.Columns(0).Name = "First Name"
.Columns(1).Name = "Last Name"
.Columns(2).Name = "Position"
.SelectionMode = DataGridViewSelectionMode.FullRowSelect
.MultiSelect = False
.Dock = DockStyle.Fill
End With
End Sub - 5). Click "Form1.vb [Design]" to view the Form in design mode and double-click the Form. Copy and paste the following code inside the "Form1_Load" subroutine to call the "DataGridViewSetup" subroutine:
DataGridViewSetup() - 6). Click "Form1.vb [Design]" to view the Form in design mode. Double-click "Button2" to open the "Button2_Click" subroutine.
- 7). Copy and paste the following code inside the "Button2_Click" to update the last row of the Data Grid View with new information and format the back and fore color of the row updated:
With Me.DataGridView1.Rows
.Item(3).Cells(0).Value = "John"
.Item(3).Cells(0).Style.BackColor = Color.Red
.Item(3).Cells(0).Style.ForeColor = Color.White
.Item(3).Cells(1).Value = "Barker"
.Item(3).Cells(1).Style.BackColor = Color.Red
.Item(3).Cells(1).Style.ForeColor = Color.White
.Item(3).Cells(2).Value = "Supervisor"
.Item(3).Cells(2).Style.BackColor = Color.Red
.Item(3).Cells(2).Style.ForeColor = Color.White
End With
Press "F5" to run your program, then click "Button1" to load the data and "Button2" to update the last row.
SHARE