locked
Adding New Custom Object with button click RRS feed

  • Question

  • Hallo,

    I have Class Customer and it has a custom object Order.

    In the form i have  

    Public customerList As New BindingList(Of Customer)

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load CustomersBindingSource.DataSource = customerList End Sub

    On button click i could add new customer like this

     CustomersBindingSource.Add(New Customer("Customer1", "Company1", "City1", "Region1", "Country1"))


    Now i would like to add new Order with button click to the current customer. I can add it in datagridview but with button i dont know how to add new order to current customer in the bindingsource.

    OrderBindingSource.Datasource = CustomersBindingSource
    OrderBindingSource.DataMember = "Order"

    i tried to add like above with customerbindingsource but it does not work


    Thanks

    Thursday, October 8, 2020 10:37 AM

Answers

  • See the following which has a person list (master), addresses (child). It's basic so there can be some assertion added. I created this from a code sample I had and modified it.

    Source code


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Shan1986 Monday, October 12, 2020 7:56 AM
    Thursday, October 8, 2020 2:58 PM

All replies

  • Now i would like to add new Order with button click to the current customer. I can add it in datagridview but with button i dont know how to add new order to current customer in the bindingsource.

    There should be an Orders collection object a List(of Order) within Customer object.  You should know what Customer object  by its index in the Customers collection bound to the grid you are on. 

    You make a new Order object and populate it. You add the Oder object to its Orders collection.

    Customer.Orders.Add(Order)

    You  can call a name a collection customerList or Customers as both names are a plural naming convention meaning more than one.

    Note: you must instance Orders as a new List(of Order) within the Customer object before you can add an Order object to the Orders collection.

    Customer.Orders = new List(of Order) 


    • Edited by DA924x Thursday, October 8, 2020 2:10 PM
    Thursday, October 8, 2020 2:06 PM
  • See the following which has a person list (master), addresses (child). It's basic so there can be some assertion added. I created this from a code sample I had and modified it.

    Source code


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Shan1986 Monday, October 12, 2020 7:56 AM
    Thursday, October 8, 2020 2:58 PM
  • Hallo Karen,

    Thanks. I have noticed that you have following in your class

        Public Property AddressList() As List(Of Address)
            Get
                Return _addressList
            End Get
            Set
                _addressList = Value
                OnPropertyChanged()
            End Set
        End Property

    What  is the difference between  this and yours and when should i use it as list?

     Public Property AddressList() As Address

    thanks

    Thursday, October 8, 2020 7:55 PM
  • Hallo Karen,

    Thanks. I have noticed that you have following in your class

        Public Property AddressList() As List(Of Address)
            Get
                Return _addressList
            End Get
            Set
                _addressList = Value
                OnPropertyChanged()
            End Set
        End Property

    What  is the difference between  this and yours and when should i use it as list?

     Public Property AddressList() As Address

    thanks

    Read up on INotifyPropertyChanged

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, October 8, 2020 8:02 PM
  • I decided to make several changes for the better.

    Changed

    Public Property AddressList() As List(Of Address)
    	Get
    	   Return _addressList
    	End Get
    	Set
      	   _addressList = Value
    	   OnPropertyChanged()
    	End Set
    End Property
    

    To this

    Public Property AddressList() As New ObservableCollection(Of Address)()
    Public Sub AddAddress(address As Address)
        AddressList.Add(address)
        OnPropertyChanged("AddressList")
    End Sub
    Why? because the change to ObservableCollection eliminates the need in the prior code to use ResetCurrent on the BindingSource.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, October 8, 2020 8:50 PM
  • Hallo Karen,

    Thanks. I have noticed that you have following in your class

        Public Property AddressList() As List(Of Address)
            Get
                Return _addressList
            End Get
            Set
                _addressList = Value
                OnPropertyChanged()
            End Set
        End Property

    What  is the difference between  this and yours and when should i use it as list?

     Public Property AddressList() As Address

    thanks

    https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/concepts/collections

    <copied>

    For many applications, you want to create and manage groups of related objects. There are two ways to group objects: by creating arrays of objects, and by creating collections of objects.

    <end>

    You are naming the property as  AddressList for a collection of related objects. But in fact,  it is not a collection of Address objects in a collection object that holds related objects in  a collection.

    You just have a property  for an Address object a single object definition, and it is not a collection object,  a container for multiple Address objects. 


    Thursday, October 8, 2020 8:52 PM
  • Hallo Karen,

    Thanks. I have noticed that you have following in your class

        Public Property AddressList() As List(Of Address)
            Get
                Return _addressList
            End Get
            Set
                _addressList = Value
                OnPropertyChanged()
            End Set
        End Property

    What  is the difference between  this and yours and when should i use it as list?

     Public Property AddressList() As Address

    thanks

    https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/concepts/collections

    <copied>

    For many applications, you want to create and manage groups of related objects. There are two ways to group objects: by creating arrays of objects, and by creating collections of objects.

    <end>

    You are naming the property as  AddressList for a collection of related objects. But in fact,  it is not a collection of Address objects in a collection object that holds related objects in  a collection.

    You just have a property  for an Address object a single object definition, and it is not a collection object,  a container for multiple Address objects. 


    And your point?

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, October 8, 2020 9:12 PM
  • Hallo Karen,

    Thanks. I have noticed that you have following in your class

        Public Property AddressList() As List(Of Address)
            Get
                Return _addressList
            End Get
            Set
                _addressList = Value
                OnPropertyChanged()
            End Set
        End Property

    What  is the difference between  this and yours and when should i use it as list?

     Public Property AddressList() As Address

    thanks

    https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/concepts/collections

    <copied>

    For many applications, you want to create and manage groups of related objects. There are two ways to group objects: by creating arrays of objects, and by creating collections of objects.

    <end>

    You are naming the property as  AddressList for a collection of related objects. But in fact,  it is not a collection of Address objects in a collection object that holds related objects in  a collection.

    You just have a property  for an Address object a single object definition, and it is not a collection object,  a container for multiple Address objects. 


    And your point?

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    That would be understand what is object oriented programming, understand naming conventions, understand what is a collection of objects as opposed to an individual object and understand the question that was asked.

    You got anything else you want to bring up?

    Thursday, October 8, 2020 11:32 PM
  • Hi Shan1986,

    How is the question going? Did you solve your problem? If your question has been answered then please click the "Mark as Answer" Link at the bottom of the correct post(s), so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, October 12, 2020 6:41 AM
  • Thanks for the answer Karen. As usual very informative and educative.
    Monday, October 12, 2020 7:57 AM
  • See the following which has a person list (master), addresses (child). It's basic so there can be some assertion added. I created this from a code sample I had and modified it.

    Source code


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Hallo Karen,

    Is there any disadvantage using Bindingsource.RemoveCurrent() to delete the item instead of using indexbased delete?

    Thanks 

    Wednesday, October 14, 2020 8:57 AM
  • Both current code, using RemoveCurrent do the same thing. I used index based as with a larger list going to the BindingList is faster. 

    We can see in the following code we can access the same list were .List is at the same level as the BindingList.

    personBindingSource.List.RemoveAt(PeopleDataGridView.CurrentCell.RowIndex)


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, October 14, 2020 11:10 AM
  • thanks karen
    Sunday, October 18, 2020 6:27 AM