Click here to Skip to main content
Licence CPOL
First Posted 11 Mar 2004
Views 111,924
Downloads 1,420
Bookmarked 19 times

Working with an unbound datagrid

By | 11 Mar 2004 | Article
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)

About the Author

ManojRajan

Web Developer

United States United States

Member

I'm ManojRajan, Working as a consultant architect in Tennessee. I have more than 8 years of experience in Microsoft Technologies.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalassign datagrid1 value to the another datagrid PinmemberGiri K17:42 20 Nov '07  
Questionhow to save those values into a database Pinmemberrsendhu20:37 4 Jun '07  
Generaledit selected dynaic column in datagrid Pinmembernishantmp119:14 1 Nov '06  
GeneralBut ... they're right! PinmemberTodd Beaulieu10:11 28 Oct '06  
Generalaaaaaaaaah! Pinmemberxoxoxoxoxoxox8:18 16 Feb '06  
This guy is hurting Gandhi's soul.
GeneralThe title of the article is not what the article describes PinsussAnonymous9:10 30 Apr '05  
Generalcomment on Working with an unbound datagrid by Coder12124 PinsussCoder1212422:11 14 Mar '04  
GeneralI love these geeks PinsussPeace2u2:23 2 Jul '04  

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.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120528.1 | Last Updated 12 Mar 2004
Article Copyright 2004 by ManojRajan
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid