Results 1 to 3 of 3

Thread: Using datagrid [Resolved]

  1. #1

    Thread Starter
    Addicted Member haihems's Avatar
    Join Date
    Oct 2004
    Posts
    150

    Resolved Using datagrid [Resolved]

    Hi,

    I get some data from Excel as a data array using the automation. I like to show those data in vb forms by using the datagrid.Shall I do that? If so , how? I need the coding..urgent!!!

    Thanks
    Last edited by haihems; Oct 20th, 2004 at 12:20 AM.

  2. #2
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036
    Hi

    The DataGrid like its name suggests is designed to display bound data only. But you use it to display unbound manually.

    the following code assume you have a datagrid on a form called DataGrid1.

    On the form class declaration put this code
    VB Code:
    1. Private m_DataTable As New DataTable("MyTableName")
    2.     Private Sub InitData()
    3.         'Set up the DataTable
    4.         m_DataTable = New DataTable("TableName")
    5.         m_DataTable.Columns.Add(New System.Data.DataColumn("Column0"))
    6.         m_DataTable.Columns.Add(New System.Data.DataColumn("Column1"))
    7.         m_DataTable.Columns.Add(New System.Data.DataColumn("Column2"))
    8.  
    9.         'Set up the datagrid control
    10.         DataGrid1.DataSource = m_DataTable
    11.     End Sub
    12.  
    13.     Private Sub AddRow(ByVal sCol0 As String, ByVal sCol1 As String, ByVal sCol2 As String)
    14.  
    15.         Dim NewRow As System.Data.DataRow = m_DataTable.NewRow()
    16.  
    17.         NewRow(0) = sCol0
    18.         NewRow(1) = sCol1
    19.         NewRow(2) = sCol2
    20.  
    21.         m_DataTable.Rows.Add(NewRow)
    22.     End Sub

    on the form_load event put this :
    VB Code:
    1. InitData()
    2.         AddRow("1", "2", "3")
    3.         AddRow("a", "b", "c")
    4.         AddRow("cat", "dog", "mouse")

    You can modify InitData and AddRow to include as many columns as you wish...

    Regards
    Jorge
    "The dark side clouds everything. Impossible to see the future is."

  3. #3

    Thread Starter
    Addicted Member haihems's Avatar
    Join Date
    Oct 2004
    Posts
    150
    Hi Jorge,

    Thanks for your reply. Its very useful for me. One more thing, can I validate the user's input in datagrid???

    regrads,
    haihems.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width