Click here to Skip to main content
15,891,951 members
Articles / Programming Languages / Visual Basic

Working with an unbound datagrid

Rate me:
Please Sign up or sign in to vote.
3.08/5 (41 votes)
11 Mar 2004CPOL 146.2K   2K   20   8
This article explains about how to manage the unbounded datagrid for storing and editing values.

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

VB
Dim Tbl As New DataTable()
'This is the column type which will be added to the Datatable
Dim Col1 As New DataColumn()
Dim Col2 As New DataColumn()
Dim Col3 As New DataColumn()

'In the general section declare a procedure to format the datatable
  Private Sub FormatGrid()

        'Define column 1
        'Specify the column data type  
        Col1.DataType = System.Type.GetType("System.Int32")
        'This name is to identify the column
        Col1.ColumnName = "No"
        'This will enable automatically increment the data 
        Col1.AutoIncrement = True
        'This value will be displayed in the header of the column
        Col1.Caption = "No"
        'This will make this column read only,Since it is autoincrement
        Col1.ReadOnly = true
        'Now add this column to the datatable
        Tbl.Columns.Add(Col1)
        'Repeat the same procedure         
        'Define Column 2
        Col2.DataType = System.Type.GetType("System.String")
        Col2.ColumnName = "Name"
        Col2.Caption = "Name"
        Col2.ReadOnly = False
        Tbl.Columns.Add(Col2)
        'Define Column 3
        Col3.DataType = System.Type.GetType("System.String")
        Col3.ColumnName = "Address"
        Col3.Caption = "Address"
        Col3.ReadOnly = False
        Tbl.Columns.Add(Col3)
      'BIND THE DATATABLE TO THE DATAGRID
       DataGrid1.DataSource = Tbl
    End Sub
'now call this procedure in the load event of the form
    FormatGrid()

' To add rows dynamically in the click event of the button, 
' include the code in the click event of the btnAdd
       'Create a row type variable

        Dim r As DataRow

        'Add a new row
        r = Tbl.NewRow()
        'Specify the  col name to add value for the row
        r("Name") = "ManojRajan"
        r("Address") = "New Delhi"
        'Add the row to the tabledata
        Tbl.Rows.Add(r)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
I'm ManojRajan, Working as a consultant architect in Tennessee. I have more than 8 years of experience in Microsoft Technologies.

Comments and Discussions

 
Generalassign datagrid1 value to the another datagrid Pin
Giri K20-Nov-07 17:42
Giri K20-Nov-07 17:42 
Questionhow to save those values into a database Pin
rsendhu4-Jun-07 20:37
rsendhu4-Jun-07 20:37 
Generaledit selected dynaic column in datagrid Pin
nishantmp11-Nov-06 19:14
nishantmp11-Nov-06 19:14 
GeneralBut ... they're right! Pin
Todd Beaulieu28-Oct-06 10:11
Todd Beaulieu28-Oct-06 10:11 
Generalaaaaaaaaah! Pin
xoxoxoxoxoxox16-Feb-06 8:18
xoxoxoxoxoxox16-Feb-06 8:18 
GeneralThe title of the article is not what the article describes Pin
Anonymous30-Apr-05 9:10
Anonymous30-Apr-05 9:10 
Generalcomment on Working with an unbound datagrid by Coder12124 Pin
Ohio81314-Mar-04 22:11
Ohio81314-Mar-04 22:11 
GeneralI love these geeks Pin
Peace2u2-Jul-04 2:23
Peace2u2-Jul-04 2:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.