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
Things you need
- Visual Basic
Start Visual Basic and create a Windows form project called "DataGridView."
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.
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."
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.
Add a Button to the form named "btnGetValue" and change the text property to "Get Value."
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
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.
- 20 of the funniest online reviews ever
- 14 Biggest lies people tell in online dating sites
- Hilarious things Google thinks you're trying to search for