Results 1 to 3 of 3

Thread: Determine type in collection in DataSource

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Determine type in collection in DataSource

    Hey,

    I am customizing a DataGridView control. The DataGridView is supposed to be bound to a collection of some type (be it a List(Of T) or ArrayList or whatever), and the idea is that one can easily determine (at design-time in this case) which properties of the type have to be shown in the grid and which column header name they should get. One can do this using an attribute. For example, if I have a class Person with Name, Age and HairColor and I don't want to show the HairColor property, and want to show the Name property as 'Person', I would use:
    vb.net Code:
    1. Public Class Person
    2.  
    3.     <Grid.GridOptions(displayName:="Person", visible:=True)> _
    4.     Public Property Name As String
    5.  
    6.     Public Property Age As Integer
    7.  
    8.     <Grid.GridOptions(visible:=False)> _
    9.     Public Property HairColor As Color
    10.  
    11. End Class

    I do this during run-time by simply looping through the properties of the bound type (in this case Person), retrieving the GridOptions attribute (if any) and checking the values provided.

    This works just fine except for one thing: I'm not really sure how to determine the type in the databound collection...

    At the moment, I cast the DataSource property to an IList and retrieve the type of the first instance in that list (if any). If there isn't a first instance (the list is empty) then I don't know how to get the type...

    Often I know that it will be impossible to know the type. If someone is binding an ArrayList for example, then it's impossible to know what kind of items he's going to put in that ArrayList. Even the normal DataGridView cannot show any columns when databound to an empty ArrayList.

    However, the framework's DataGridView does show the columns when bound to, for example, an empty List(Of Person), or a Person array.

    How does it do this? And more importantly: how can I do this?

    I'm pretty sure that casting the DataSource to an IList is not the correct way as all information about the type of items in that list is then lost. But I'm not sure what else to cast it to... ITypedList? IBindingList? I even considered List(Of T) because that's all I am going to be using, but besides the fact that the grid would then obviously only work for a List(Of T) I still don't know how to do this... I don't know 'T' so I can't cast it to a List(Of T)...

    I'm a little lost

  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Determine type in collection in DataSource

    Hey Nick,

    Is this what you are looking for?

    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
    4.         MyBase.OnLoad(e)
    5.         Dim source = New List(Of String)()
    6.  
    7.         Me.DataGridView1.DataSource = source
    8.  
    9.         Dim type = Me.GetFirstSourceTypeOrDefault(Me.DataGridView1)
    10.  
    11.         If type IsNot Nothing Then
    12.             MessageBox.Show(type.ToString())
    13.         End If
    14.  
    15.     End Sub
    16.  
    17.     Public Function GetFirstSourceTypeOrDefault(ByVal grid As DataGridView) As Type
    18.         Dim source = TryCast(grid.DataSource, IList)
    19.         If source IsNot Nothing Then
    20.             Dim type = source.GetType()
    21.             If type.IsGenericType() Then
    22.                 Return type.GetGenericArguments().FirstOrDefault()
    23.             End If
    24.         End If
    25.         Return Nothing
    26.     End Function
    27.  
    28. End Class

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Determine type in collection in DataSource

    That looks promising, I didn't know about IsGenericType.

    I also see now that there is a Type.GetTypeArray method that should tell me the type of the objects in an array. I suppose I could check if the type is an array and use that if it's not a generic type.

    I'll try it out later, thanks!

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