- This topic explains about the working of datagridview in VB programming.
- Tools needed: DataGridView (dataGridView1), Button1 (btnColumnADD), Button2 (btnRowsAdd), Button3 (btnRowsCount), Button4 (btnColumnCount), Button5 (btnAddGridValues), button6 (btnGetValues)
DataGridView – DataGridView in VB
- The DataGridView control provides a customizable table for displaying data.
- DataGridView control is designed to be a complete solution for displaying tabular data with Windows Forms.
- DataGridView class allows us to customization of cells, rows, columns, and borders through the use of its properties
- Each cell within the DataGridView control can have its own style, such as text format, background color, foreground color, font etc.
- All cells derive from the DataGridViewCell base class.
Create Column dynamically in DataGridView – DataGridView in VB
|
1 2 3 |
DataGridView1.Columns.Add(0, "ColumnName1") '0 - index DataGridView1.Columns.Add(1, "ColumnName2") '1 - index DataGridView1.Columns.Add(2, "ColumnName3") '2 – index |
- 0,1,2,….. are the index of the datagridview control.
- Index starts with 0 (Zero) in datagridview.
Create rows in DataGridView – DataGridView in VB
- Rows must be added only after creating columns, otherwise error will be occurred.
|
1 |
DataGridView1.Rows.Add() |
Count No: of rows in DataGridView – DataGridView in VB
|
1 |
DataGridView1.Rows.Count |
Count no: of columns in DataGridView – DataGridView in VB
|
1 |
DataGridView1.Columns.Count |
Add data to the DataGridView – DataGridView in VB
|
1 2 3 |
DataGridView1.Rows(0).Cells(0).Value = "Hello" DataGridView1.Rows(0).Cells(1).Value = "Dear" DataGridView1.Rows(0).Cells(2).Value = "Friend" |
- Rows[0] – index of the row
- Cells[0] – index of the column
Get data from DataGridView – DataGridView in VB
|
1 |
DataGridView1.Rows(0).Cells(0).Value.ToString() |
Clear rows in DataGridView – DataGridView in VB
|
1 |
DataGridView1.Rows.Clear() |
Clear Columns in DataGridView - DataGridView in VB
|
1 |
DataGridView1.Columns.Clear() |
DataGridView in VB – Complete Code
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
Public Class Form1 Private Sub btnColumnADD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnColumnADD.Click DataGridView1.Columns.Add(0, "ColumnName1") '0 - index DataGridView1.Columns.Add(1, "ColumnName2") '1 - index DataGridView1.Columns.Add(2, "ColumnName3") '2 - index End Sub Private Sub btnRowsAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRowsAdd.Click DataGridView1.Rows.Add() End Sub Private Sub btnRowsCount_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRowsCount.Click MsgBox(DataGridView1.Rows.Count, MsgBoxStyle.Information, "Rows Count") End Sub Private Sub btnColumnCount_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnColumnCount.Click MsgBox(DataGridView1.Columns.Count, MsgBoxStyle.Information, "Column Count") End Sub Private Sub btnAddGridValues_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddGridValues.Click DataGridView1.Rows(0).Cells(0).Value = "Hello" DataGridView1.Rows(0).Cells(1).Value = "Dear" DataGridView1.Rows(0).Cells(2).Value = "Friend" End Sub Private Sub btnGetValues_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetValues.Click MsgBox(DataGridView1.Rows(0).Cells(0).Value.ToString(), MsgBoxStyle.Information, "Result") End Sub End Class |
DataGridView in VB
If you have any suggestions or doubts regarding this topic, please contact us…













