Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1898614
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:47:13+00:00 2026-05-17T06:47:13+00:00

This a data binding question in C#. I have a simple Person class: public

  • 0

This a data binding question in C#.

I have a simple Person class:

public class Person {
    public string Name {get; set;}
    public string Level {get; set;}
}

And I have a Group class, which contains a Persons property as a BindingList of Person instances:

public class Group {
    private BindingList<Person> persons_;
    public BindingList<Person> Persons {
            get { return persons_; }
            set { persons_ = value; }
    }
}

Finally, I use a BindingSource to connect a group instance to a DataGridView:

// Instantiate a Group instance.
Group p = new Group();
p.Persons = new BindingList<Person> {
    new Person {Name = "n1", Level = "l1"},
    new Person {Name = "n2", Level = "l2"},
    new Person {Name = "n3", Level = "l3"}
};

BindingSource bs = new BindingSource();
bs.DataSource = p;
bs.DataMember = "Persons";
DataGridView dgv = new DataGridView();
dgv.DataSource = bs;

After the above code, the DataGridView will display three Person instances. Everything works fine. Then I have a button, when the button is clicked, I create another Person BindingList and use it to replace p.Persons:

p.Persons = new BindingList<Person> {
    new Person {Name = "n10", Level = "l10"},
    new Person {Name = "n11", Level = "l11"},
    new Person {Name = "n12", Level = "l12"}
};

After the above code, the data binding stops working. Adding ResetBindings() calls from either the BindingSource or the DataGridView doesn’t solve the break of the data binding.

Since my BindingSource is bound to the Persons property of the Group class, I think the change of the Persons property should not break the data binding but in fact it does. But this is what we normally do if binding a string property to a Label or TextBox. We just assign a new instance of string to the property and the Label or TextBox will update itself properly. Why this doesn’t work for collections of instances? What is the correct code to achieve the same behavior? Thank you very much.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T06:47:14+00:00Added an answer on May 17, 2026 at 6:47 am

    Your data model is very wrong: Collection properties should be readonly.

    Instead, you should remove the Persons setter, then clear the list and call Add repeatedly.
    If you want to, you can even create an extension method

    public static void  ReplaceWith<T>(this IList<T> list, params T[] newItems)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have entity class: public class Project { public virtual int Id { get;
I have a simple class with a property class Foo { string Title {
I have a very simple WPF application in which I am using data binding
This seems like a fundamentally simple question. I have a WinForms dialog box with
I have a question about data binding in WP7 using c# code. If I
How do I use data-binding from code (C# or VB)? This is what I
I'm working through the Demo: Binding Data with the SqlDataSource of this pluralsight video
This data is for a holiday cottage's simple accommodation calendars. The data is simple
I have this data in cell A1: Majestic Properties Design District, LLC, Ste. 101,
Let's say we have data as follows var data = { facets: [{ name

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.