Introduction
This article explains about how to manage the unbounded datagrid for storing and editing values. In earlier versions of Visual Basic, We make use of MS Flex Grid to manipulate the spreadsheet type of data. In Vb.Net you can use the datagrid for the same purpose and can add columns and rows dynamically. Let us see this in the example given below
- Open a VisualBasic Project
- Drop a Datagrid in the design view
- Drop a Button
- Change the button name property to btnAdd and text property to Add
- Now in the general declaration section of the class declare these variables
Example
'This is the table which will hold our columns and rows
Dim Tbl As New DataTable()
Dim Col1 As New DataColumn()
Dim Col2 As New DataColumn()
Dim Col3 As New DataColumn()
Private Sub FormatGrid()
Col1.DataType = System.Type.GetType("System.Int32")
Col1.ColumnName = "No"
Col1.AutoIncrement = True
Col1.Caption = "No"
Col1.ReadOnly = true
Tbl.Columns.Add(Col1)
Col2.DataType = System.Type.GetType("System.String")
Col2.ColumnName = "Name"
Col2.Caption = "Name"
Col2.ReadOnly = False
Tbl.Columns.Add(Col2)
Col3.DataType = System.Type.GetType("System.String")
Col3.ColumnName = "Address"
Col3.Caption = "Address"
Col3.ReadOnly = False
Tbl.Columns.Add(Col3)
DataGrid1.DataSource = Tbl
End Sub
FormatGrid()
Dim r As DataRow
r = Tbl.NewRow()
r("Name") = "ManojRajan"
r("Address") = "New Delhi"
Tbl.Rows.Add(r)
I'm ManojRajan, Working as a consultant architect in Tennessee. I have more than 8 years of experience in Microsoft Technologies.