Click here to Skip to main content
Click here to Skip to main content
Go to top

Working with an unbound datagrid

, 11 Mar 2004
Rate this:
Please Sign up or sign in to vote.
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

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)

Share

About the Author

ManojRajan
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 PinmemberGiri K20-Nov-07 17:42 
Questionhow to save those values into a database Pinmemberrsendhu4-Jun-07 20:37 
Generaledit selected dynaic column in datagrid Pinmembernishantmp11-Nov-06 19:14 
GeneralBut ... they're right! PinmemberTodd Beaulieu28-Oct-06 10:11 
Generalaaaaaaaaah! Pinmemberxoxoxoxoxoxox16-Feb-06 8:18 
GeneralThe title of the article is not what the article describes PinsussAnonymous30-Apr-05 9:10 
Generalcomment on Working with an unbound datagrid by Coder12124 PinsussCoder1212414-Mar-04 22:11 
GeneralI love these geeks PinsussPeace2u2-Jul-04 2:23 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 12 Mar 2004
Article Copyright 2004 by ManojRajan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid