How to Update a Row in Grid View

Written by jaime avelar
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Update a Row in Grid View
Update a row in a Data Grid View control using VB.NET. (background with binary data image by Pedro Nogueira from Fotolia.com)

Knowing how to update a row in a Data Grid View control using Microsoft Visual Basic .NET can help you dynamically display data to the user. Often programmers run into the need to change data in a Data Grid View control while their program is running. VB.NET is an object-oriented computer programming language engineered by Microsoft. A Data Grid View control works similar to a spreadsheet. It uses columns and rows to associate and display data to a user.

Skill level:
Moderate

Other People Are Reading

Things you need

  • Microsoft Visual Basic Express

Show MoreHide

Instructions

  1. 1

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

  2. 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. 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 row 0 As String() = {"Ana", "Lopez", "Teacher"}

        Dim row 1 As String() = {"Sylvia", "Gonzalez", "Business Admin"}
    
        Dim row 2 As String() = {"Jaime", "Melendrez", "RF Engineer"}
    
        Dim row 3 As String() = {"Mary", "Smith", "Manager"}
    
    
    
        With Me.DataGridView1.Rows
    
            .Add(row0)
    
            .Add(row1)
    
            .Add(row2)
    
            .Add(row3)
    
        End With
    
  4. 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. 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. 6

    Click "Form1.vb [Design]" to view the Form in design mode. Double-click "Button2" to open the "Button2_Click" subroutine.

  7. 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 colour 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.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

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