Results 1 to 4 of 4

Thread: ComboBox not filled with DataTable - Why?

  1. #1

    Thread Starter
    Member VBobCat's Avatar
    Join Date
    Sep 2011
    Location
    São Paulo, Brazil
    Posts
    34

    Unhappy ComboBox not filled with DataTable - Why?

    I used this code:

    vb Code:
    1. Friend Sub PopulateCB(ByRef mycontrol As ComboBox, _
    2.                       ByVal expressionSQL As String)
    3.     ' DMBD is my data-handling class:
    4.     Using DataTableOrigem As DataTable = DMBD.Read_a_Table(expressionSQL)
    5.         ' this MsgBox told me my Table is correctly filled:
    6.         MsgBox(DataTableOrigem.Rows(0).Item(0).ToString)
    7.         ' this MsgBox told me my control was correctly passed:
    8.         MsgBox(mycontrol.Name)
    9.         mycontrol.DataSource = DataTableOrigem
    10.         mycontrol.ValueMember = DataTableOrigem.Columns(0).ColumnName
    11.         mycontrol.DisplayMember = DataTableOrigem.Columns(1).ColumnName
    12.     End Using
    13. End Sub

    Nevertheless, mycontrol remains empty after this.

    Could someone help me figure out why?

    Thank you very much!

  2. #2
    Hyperactive Member mbutler755's Avatar
    Join Date
    May 2008
    Location
    Peoria, AZ
    Posts
    417

    Re: ComboBox not filled with DataTable - Why?

    I would use a binding source and table adapter to accomplish this. Which version of VS are you using?
    Regards,

    Matt Butler, MBA, BSIT/SE, MCBP
    Owner, Intense IT, LLC
    Find us on Facebook
    Follow us on Twitter
    Link up on LinkedIn
    mb (at) i2t.us

    CODE BANK SUBMISSIONS: Converting Images to Base64 and Back Again

  3. #3

    Thread Starter
    Member VBobCat's Avatar
    Join Date
    Sep 2011
    Location
    São Paulo, Brazil
    Posts
    34

    Re: ComboBox not filled with DataTable - Why?

    I am not so sure about how to do so...

    What I can say, in order to clarify, is this: my data source is an Access 12 file (.accdb), and all the OleDb commands I use to handle it are encapsulated in that DMDB class. I need to fill this ComboBox which data that are loaded in application's startup, and remain in disconnected DataTable, for it will not change along runtime (they are actually long category lists, so I decided to load them only once, to avoid network traffic overhead).

    I am using VS 2010 Express, over VB.NET, Framework version #4.

    Thank you very much!

  4. #4

    Thread Starter
    Member VBobCat's Avatar
    Join Date
    Sep 2011
    Location
    São Paulo, Brazil
    Posts
    34

    Thumbs up SOLVED: ComboBox not filled with DataTable - Why?

    My own mistake. I just have been told about it. Using statement disposes the used resource at end of block, so I have disposed the DataTable and, with it, all ComboBox data, which was bound and referred to that DataTable.

    I've already tested the fixed code. It made all ok and it is, also, far more elegant:

    vb Code:
    1. Friend Sub PopulateCB(ByRef mycontrol As ComboBox, ByVal expressionSQL As String)
    2.      mycontrol.DataSource = DMBD.Read_a_Table(expressionSQL)
    3.      mycontrol.ValueMember = DirectCast(mycontrol.DataSource, DataTable).Columns(0).ColumnName
    4.      mycontrol.DisplayMember = DirectCast(mycontrol.DataSource, DataTable).Columns(1).ColumnName
    5. End Sub

    Thanks to all of you that spent some thinking over the question!
    Last edited by VBobCat; Dec 15th, 2011 at 01:57 PM.

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