Hi Gary,
Thank you for your response.
Could you tell me what kind of data source you're using? I guess it is a
list of custom objects, right?
Only those data source that implements the IBindingList interface can
support change notification for when the list itself changes(for example,
the number of items in the list increases or decreases). BindingList<T>
provides a generic implementation of the IBindingList interface, so it is
a
good candidate to use as the data source.
Note that a class must implement the INotifyPropertyChanged interface to
make change notification when any of its property values change.
To summary, I suggest that you use the BindingList<T> as the data source
and implement the INotifyPropertyChanged interface for your business
class.
I will illuminate this with a sample. It requires that you add a
DataGridView and two Buttons on the form.
public partial class Form1 : Form
{
private void Form1_Load(object sender, EventArgs e)
{
BindingList<Person> lists = new BindingList<Person>();
lists.Add(new Person("1", "aa"));
lists.Add(new Person("2", "bb"));
this.dataGridView1.DataSource = lists;
}
private void button1_Click(object sender, EventArgs e)
{
BindingList<Person> lists = this.dataGridView1.DataSource as
BindingList<Person>;
lists.Add(new Person("3", "22"));
}
private void button2_Click(object sender, EventArgs e)
{
BindingList<Person> lists = this.dataGridView1.DataSource as
BindingList<Person>;
lists[1].ID = "5";
}
class Person:INotifyPropertyChanged
{
string id;
string name;
public string ID
{
get { return id; }
set
{
id = value;
if (PropertyChanged != null)
PropertyChanged(this, new
PropertyChangedEventArgs("ID"));
}
}
public string Name
{
get { return name; }
set
{
name = value;
if (PropertyChanged != null)
PropertyChanged(this, new
PropertyChangedEventArgs("Name"));
}
}
public Person(string _id,string _name)
{
id = _id;
name = _name;
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
Run the program. When you click the button1, you should see a new row is
added in the DataGridView. When you click the button2, you should see the
value of column 'ID' in the second row is changed to 5.
For more information on interfaces related to data binding, you may read
the following MSDN document:
'Interfaces Related to Data Binding '
http://msdn2.microsoft.com/en-us/library/41e17s4b.aspx
BTW, the Refresh or Invalidate method is used to cause the DataGridView to
be redrawn. It's no use calling either of these two methods to get the
DataGridView to show new data in the data source.
Alternatively, if you're using BindingSource as the data source, i.e. bind
the BindingSource to a list of custom objects and then bind the
DataGridView to the BindingSource, you could call the ResetBindings method
of the BindingSource to cause the DataGridView to reread all the items in
the list and refresh their displayed values. The following is a sample.
List<Person> lists = new List<Person>();
// add some objects in the lists
....
BindingSource bs = new BindingSource(lists, "");
dataGridView1.DataSource = bs;
lists.Add(new Person("6","cc"));
bs.ResetBindings(false);
I need the AutoGenerateColumns set to generate Columns because I want to
specify specific attributes such as width, etc at design time.
You have a misunderstanding here. The AutoGenerateColumns property is used
to indicate whether columns are created automatically when the DataSource
or DataMember properties are set at run time. We can only set the
AutoGenerateColumns property in code, because this property is not
available in the Properties window.
Hope this helps.
If you have any question, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support