Results 1 to 3 of 3

Thread: [VB/WPF] How to populate a Datagrid from a Dictionary Of Class

  1. #1

    Thread Starter
    Member Abelius's Avatar
    Join Date
    Nov 2011
    Posts
    44

    Post [VB/WPF] How to populate a Datagrid from a Dictionary Of Class

    Hi there,

    I'm creating a simulation game with RPG and Visual Novel elements. One of its primary requirements is that it supports overlapping of alpha transparent PNG images, so I switched to WPF for its seamless functionality in that respect.

    However... I'm amazed at how my previous WinForms code has become useless at some parts, but I was able to cope until now. I'm banging my head with something that should be easy (in WinForms it was) to do: populate a Datagridview control with data from the main dictionary of class of the game.

    However, my previous code is now useless because Datagrids in WPF doesn't have a Rows.Add method. This is what I had in WinForms...:
    Code:
    Public Sub PopulateDebugPersonsDataGrid()
        datagridDebug.Rows.Clear()
        For n As Integer = 1 To students_count
            datagridDebug.Rows.Add({n, studentList("student" & n).firstname, studentList("student" & n).lastname
        Next
    End Sub
    Where...:
    studentList is a Dictionary(of Person) created at run time (new key,Person pairs could be added later).
    students_count is an integer variable containing the current number of Person instances.
    datagridDebug is a Datagridview control made at design time with all of its headers filled in.

    This is a simplified version for the sake of visibility because that Person class has a lot more properties.

    Well, my problem is that WPF doesn't support this method and it seems that I need to "bind a datasource" or something like that. I've came across a ton of posts and articles in the past 24 hours and the best I managed to get was a grid with two columns: one displaying the dictionary keys (student1, student2...) and another for the supposed value (HHS.Person, HHS.Person) and that only because I left the Datagrid property "AutoGenerateColumns" set to True.

    And needless to say, what I really need to display at the Datagrid are the firstname, lastname properties, not the name of the whole class.

    Worse even, the majority of tutorials out there take for granted that if you are working with WPF then you are coding with C#... I'm really sick of getting my hopes high because a Google result seems promising to only realize that it's full of those dammed { and } symbols all over the code behind.

    So I'll be really grateful if you guide me in the right direction.

    I know this is not the first not the last time I'll say this but... it shouldn't be very strange what I'm intending to do right?

  2. #2
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,021

    Re: [VB/WPF] How to populate a Datagrid from a Dictionary Of Class

    WPF is a different ballgame and you don't expect everything that's working in Windows Forms to fit in with WPF. As with how you add rows in WPF, you do it with Items.Add(). I'll provide you with links in C#. Even though you don't like the {} syntax, it will point you to the right direction.

    See the answer with an example class called Item.
    Programatically add column & rows to WPF Datagrid

    Bind Dictionary to WPF DataGrid.
    http://stackoverflow.com/questions/5...-string-stringFilling WPF DataGrid in C# with a Dictionary

    I also have wpf code bank submissions in my signature.

    - kgc
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  3. #3

    Thread Starter
    Member Abelius's Avatar
    Join Date
    Nov 2011
    Posts
    44

    Re: [VB/WPF] How to populate a Datagrid from a Dictionary Of Class

    Thank you for your help! I'll try to make use of those links tomorrow.

    About the C# overwhelming presence... well, I suppose that's the way it is now in 2016. :-D In fact, the VB course in my college was dropped by the faculty last year... I imagine it's not so popular nowadays.

    Maybe it's time to switch to C# after all... :-/

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