Results 1 to 6 of 6

Thread: [RESOLVED] access a DataTable which created at runtime

  1. #1

    Thread Starter
    Junior Member iRoN_RoCK's Avatar
    Join Date
    Jul 2008
    Posts
    31

    Resolved [RESOLVED] access a DataTable which created at runtime

    Hello guys,
    I created a Datatable at runtime, added data to it succesfully but now i cant access it. For example, I can't filter by nationality
    Code:
    Public DtPlayers As New DataTable("PlayerNames")
    
    Private Sub frmMain_Load(...)
     Do Until cnt=playerCount
       ...
       DtPlayers.Rows.Add(New Object() {cnt, PlID, PlName, vbNullString, _
       PlAge, PlNationality,  PlHeight, PlWeight, PlPosition})
       cnt=cnt+1
     Loop
    end sub   
    
    Private Sub TestGrid_ColumnHeaderMouseClick(...)
            Dim myQuery = From Player In DtPlayers _
                        Where _(what should i add here?)_ = "Turkey" _
                        Select Player
        End Sub
    Thanks in advance,
    iRoN_RoCK

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: access a DataTable which created at runtime

    That's because it's not a typed datatable.... so LINQ has no way of knowing what fields are in the datatable. But... that just means you don't get the nicety of intellisense... It's still possible to extract field values....

    I think this is what you are looking for:

    Code:
     Where DtPlayers.Item("stuff your nationality field name here").ToString = "Turkey" _
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Junior Member iRoN_RoCK's Avatar
    Join Date
    Jul 2008
    Posts
    31

    Re: access a DataTable which created at runtime

    Quote Originally Posted by techgnome View Post
    That's because it's not a typed datatable.... so LINQ has no way of knowing what fields are in the datatable. But... that just means you don't get the nicety of intellisense... It's still possible to extract field values....

    I think this is what you are looking for:

    Code:
     Where DtPlayers.Item("Nationality").ToString = "Turkey" _
    -tg
    'Item' is not a member of 'System.Data.DataTable' it said
    Last edited by iRoN_RoCK; Apr 10th, 2009 at 07:49 AM.

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: access a DataTable which created at runtime

    Try Player instead of DtPlayers .... sorry... I'm used to typed Datatables, so I'm not used to this.... if that still doesn't work... try this:

    Code:
    Where (function(s) s.Item("stuff your nationality field name here").ToString = "Turkey") _
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Junior Member iRoN_RoCK's Avatar
    Join Date
    Jul 2008
    Posts
    31

    Re: access a DataTable which created at runtime

    Quote Originally Posted by techgnome View Post
    Try Player instead of DtPlayers .... sorry... I'm used to typed Datatables, so I'm not used to this.... if that still doesn't work... try this:

    Code:
    Where (function(s) s.Item("stuff your nationality field name here").ToString = "Turkey") _
    -tg
    this one was ok but it just cleared all rows from datagridview. what did i do wrong?
    Code:
            Dim myQuery = From Player In DtPlayers _
                        Where Player.Item("Nationality").ToString = "Turkey" _
                        Select Player
            TestGridView.DataSource = myQuery

  6. #6

    Thread Starter
    Junior Member iRoN_RoCK's Avatar
    Join Date
    Jul 2008
    Posts
    31

    Re: access a DataTable which created at runtime

    This one works. Thanks for your posts anyway
    Code:
            Dim dv = New DataView(DbPlayers)
            dv.RowFilter = "Nationality = 'Turkey'"
            TestGridView.DataSource = dv

Tags for this Thread

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