Databind to collection object

I've created a customized collection and want to databind it to a DataGridView. After some research, I found that I need to create a bindingsource control and then bind to the specific collection object.

The first problem is that I need to retrieve the collection instance for some additional method calls. How can I get the underlying collection instance back I found that the databingsource will use BindingList<T> as its internal store. How can I convert it back to my specific collection so that I can make additional method call


Answer this question

Databind to collection object

  • John Winstanley

    as I am not sure what Total is based on. . . I will assume FooBar has a double property:  Value. . .

    Public Class FoobarCollection 
          Inherits BindingList(Of FooBar)
          '' define your methods here if they 
          '' aren't already handled via the 
          '' generic mechanism 
          Public ReadOnly Property Total as Double
             Get
                  Dim temp as Double = 0 
                  For Each foo as Foobar in Me
                      temp += foo.Value
                  Next 
                  Return temp
             End Get 
          End Property
    End Class



  • Saurabh Dhall

    ok. . . I made this. . .

  • Colin Doran

    The message box just have showed runtimetype.



  • BerndMei

    Return to my first question, how can I retrieve this collection from the databindingsouce so that I can get this property I've tried something like this.

    Dim lTenderItemCollection As TenderItemCollection = CType(Me.TenderItemsListBindingSource.DataSource, TenderItemCollection)
    Me.paidTotalTextBox.Text = lTenderItemCollection.GetPaidTotal

    This doesn't work as the CType has failed to make the cast.



  • JohnInTampa

    what does this code show you. . .

    MessageBox.Show(Me.TenderItemsListBindingSource.DataSource.GetType().Name)



  • Pat84

    public class FoobarCollection
    inherits BindingList(Of FooBar)
    '' define your methods here if they
    '' aren't already handled via the
    '' generic mechanism
    end class

    [notice how much cleaner the c# is ]



  • B.H.V.Ramakrishna

    ahh. . . yes . . .hold on somtimes I dont pay attention. . . give me a moment

  • Pete Bennett

    Belair, thanks for your help first. Actually, I've just made the same thing worked. (Certainly with your help) However, referring to the first message, I'm actually working on a DataBindingSource. The databindingsource use the TenderItemCollection object as the datasource. The DataBindingSource will create an instance by itself. The code that you show is putting an instance into the DataBindingSource instead of the self-gerenrated one. I hope that you understand the difference that I want to point out.

    If I use the above coding, actually I don't need to get the TenderItemCollection out from the DataGridView. Instead I can use the bl directly.

    The reason that I would like to do this because it'll be easier to configure the DataGridView's columns if the DataBindingSource is configured with a datasource.



  • Lyndon J Clarke

    Is there any equivalent way to do this in VB.NET

  • Taby

    your custom collection should inherit from BindingList. . .

    if you have a class FooBar. . . and a collection FooBarCollection

    change the declaration for FoobarCollection to

  • fastcarrera

    I know. . . and what is the runtime type

  • Jason Browne

    Thanks for your advise first. I can't make the above code work at first because I've not added the namespace System.ComponentModel. After I import it, the above syntax works.

    As I need to write a function that calculate the total stored in each item, so is there any way to do so I found that there's a property called Items. I hope it's what I need.

    Another question is how can I get back this list from databindingsource and call the function that I need

    Thanks Blair!



  • Alex_M

    In the debug mode, I found its name is TenderItemCollection. This is the type that I'm working on but I can't cast it to TenderItemCollection and get the property.

  • JaLeo

    oops. . . there was an error in my code. . . its fixed

  • Databind to collection object