How to Get Data From DataGridView in Visual Basic Net

Written by allen stoner
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Get Data From DataGridView in Visual Basic Net
Office workers pulling data from a datagrid view. (Keith Brofsky/Digital Vision/Getty Images)

The DataGridView control is a useful control for displaying data to the user in a grid format. It can be bound to a data source or used unbound. Either way it is used it may be necessary to pull a value out of a cell to use for some other purpose. There are several ways to do this, but it's easy if you just know the row and column of the cell from which to pull the data.

Skill level:
Moderately Easy

Other People Are Reading

Things you need

  • Visual Basic

Show MoreHide

Instructions

  1. 1

    Start Visual Basic and create a Windows form project called "DataGridView."

  2. 2

    Add a DataGridView control to the form. It can be found in the "Data" section of the "Toolbox." Dock the control to the top of the form.

  3. 3

    Right click on the "DataGridView" object and "Add Columns." Add two "Unbound" columns to the grid, name the first one "Col1" and give it a header text of "Column 1." Add a second column as "Col2" with header text of "Column 2."

  4. 4

    Add two Labels and two NumericUpDown controls to the form. Give the labels the text of "Column" and "Row." Change the names of the NumericUpDown controls to "nudRow" and "nudColumn." Arrange these controls on the form under the DataGridView lined up accordingly.

  5. 5

    Add a Button to the form named "btnGetValue" and change the text property to "Get Value."

  6. 6

    Double click on the button and add this code to its action:

    Private Sub btnGetValue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetValue.Click
    
        MsgBox(DataGridView1.Item(CInt(nudColumn.Value), CInt(nudRow.Value)).Value.ToString)
    
    End Sub
    
  7. 7

    Run the program and add some data to the grid. At least make sure the first row is filled in and saved. Click the button with both NumericUpDown controls still at "0." The value that you typed into the first cell should appear in the message box that pops up. Remember the grid is zero-based, so the top left cell is (0,0).

Tips and warnings

  • Add some error handling or the call will fail if it points outside the existing grid.

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.