Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# DataGridView not updated when datasource is changed

Tags:

c#

winforms

I have a list of object

List<MobilePhone> results;

so i added the list to the datagridview

dataGridView.DataSource = phase3Results;

so i have a few dropdown boxes which dictate the list results at any change of selected item in the dropdown boxes, so my list results changes, but on the datagridview its not reflected. is there any way to "refresh" the changes?

like image 371
kkh Avatar asked Mar 18 '12 13:03

kkh


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


2 Answers

Quick and dirty solution:

dataGridView.DataSource = null;
dataGridView.DataSource = phase3Results;

Clean and correct solution:

Use a BindingList<T> instead of List<T> as your DataSource. List<T> does not fire events when its collection changes.

Also, if you additionally implement INotifyPropertyChanged for T, BindingList<T> automatically subscribes to property changes for each T in the collection and lets the view know about the change.

like image 155
Saeb Amini Avatar answered Oct 24 '22 17:10

Saeb Amini


Try using a BindingList<> instead of List<> and (as already suggested by Daniel), implement INotifyPropertyChanged. However, I think you can also call .Refesh() if you didn't want to implement the INotifyPropertyChanged interface.

Here's an example ripped from here

public class Car : INotifyPropertyChanged
 {
   private string _make;
   private string _model;
   private int _year;

  public event PropertyChangedEventHandler PropertyChanged;

  public Car(string make, string model, int year)
   {
     _make = make;
     _model = model;
     _year = year;
   }

  public string Make
   {
     get { return _make; }
     set
     {
       _make = value;
       this.NotifyPropertyChanged("Make");
     }
   }

  public string Model
   {
     get { return _model; }
     set
     {
       _model = value;
       this.NotifyPropertyChanged("Model");
     }
   }

  public int Year
   {
     get { return _year; }
     set
     {
       _year = value;
       this.NotifyPropertyChanged("Year");
     }
   }

  private void NotifyPropertyChanged(string name)
   {
     if(PropertyChanged != null)
       PropertyChanged(this, new PropertyChangedEventArgs(name));
   }
 }

_dgCars.AutoGenerateColumns = false;

DataGridViewTextBoxColumn makeColumn = new DataGridViewTextBoxColumn();
 makeColumn.DataPropertyName = "Make";
 makeColumn.HeaderText = "The Car's Make";

DataGridViewTextBoxColumn modelColumn = new DataGridViewTextBoxColumn();
 modelColumn.DataPropertyName = "Model";
 modelColumn.HeaderText = "The Car's Model";

DataGridViewTextBoxColumn yearColumn = new DataGridViewTextBoxColumn();
 yearColumn.DataPropertyName = "Year";
 yearColumn.HeaderText = "The Car's Year";

_dgCars.Columns.Add(makeColumn);
 _dgCars.Columns.Add(modelColumn);
 _dgCars.Columns.Add(yearColumn);

BindingList<Car> cars = new BindingList<Car>();

cars.Add(new Car("Ford", "Mustang", 1967));
 cars.Add(new Car("Shelby AC", "Cobra", 1965));
 cars.Add(new Car("Chevrolet", "Corvette Sting Ray", 1965));

_dgCars.DataSource = cars;
like image 26
Chris Gessler Avatar answered Oct 24 '22 19:10

Chris Gessler