C# Using DataGridView with BindingSource.DataSource=List - underlying data not refressing databindin

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I am looking to resolve an update issue between my underlying data object to a datagridview using a BindingSource and has an AddItem_Click() method. Below is my scenario.
<br/>
public partial class Form1 : Form {<br/>
private BindingSource bindingSource1;<br/>
private MyRecordOfFields myLocalFields;
public Form1() {<br/>
InitializeComponent();<br/>
}
private void Form1_Load(object sender, EventArgs e) {<br/>
myLocalFields = new MyRecordOfFields(null);<br/>
MyField field1 = new MyField(null);<br/>
field1.MyFieldName = "Field_1";<br/>
field1.MyFieldDescription = "Description1";<br/>
field1.Size = 3;<br/>
myLocalFields.MyMembers.Add(field1);<br/>
MyField field2 = new MyField(null);<br/>
field2.MyFieldName = "Field_2";<br/>
field2.MyFieldDescription = "Description2";<br/>
field2.Size = 3;<br/>
myLocalFields.MyMembers.Add(field2);<br/>
MyField field3 = new MyField(null);<br/>
field3.MyFieldName = "Field_3";<br/>
field3.MyFieldDescription = "Description3";<br/>
field3.Size = 3;<br/>
myLocalFields.MyMembers.Add(field3);
bindingSource1 = new BindingSource();<br/>
bindingSource1.AllowNew = true;<br/>
<br/>
dataGridView1.AutoGenerateColumns = false;<br/>
int colIndex = dataGridView1.Columns.Add("MyFieldName", "MyFieldName");<br/>
dataGridView1.Columns[colIndex].DataPropertyName = "MyFieldName";<br/>
colIndex = dataGridView1.Columns.Add("MyFieldDescription", "MyFieldDescription");<br/>
dataGridView1.Columns[colIndex].DataPropertyName = "MyFieldDescription";<br/>
colIndex = dataGridView1.Columns.Add("Size", "Size");<br/>
dataGridView1.Columns[colIndex].DataPropertyName = "Size";<br/>
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;<br/>
dataGridView1.AutoResizeColumns();
bindingSource1.DataSource = myLocalFields.MyMembers.Cast<MyField>();<br/>
<br/>
dataGridView1.DataSource = bindingSource1;<br/>
dataGridView1.Invalidate();
}
private void AddItem_Click(object sender, EventArgs e) {<br/>
MyField field1 = new MyField(null);<br/>
field1.MyFieldName = "Field_" + (myLocalFields.MyMembers.Count + 1);<br/>
field1.MyFieldDescription = "Description" + (myLocalFields.MyMembers.Count + 1);<br/>
field1.Size = myLocalFields.MyMembers.Count + 1;<br/>
myLocalFields.MyMembers.Add(field1);<br/>
<br/>
((BindingSource)dataGridView1.DataSource).ResetBindings(false);<br/>
dataGridView1.Invalidate();<br/>
}<br/>
}
public interface MyElement : IEditableObject {<br/>
Object createInstance(MyElement parent);<br/>
string Name { get; set; } <br/>
string Description { get; set; } <br/>
}
abstract public class MyElementAdapter : MyElement { <br/>
private string myName; <br/>
private string myDescription; <br/>
private MyElement myParent;<br/>
public virtual Object createInstance(MyElement parent) {<br/>
Type t = this.GetType();<br/>
Object o = (Object)Activator.CreateInstance(t, new object[] { parent });<br/>
return o;<br/>
}<br/>
public MyElementAdapter(MyElement parent) {
<br/>
myName = "New_Element";
<br/>
myDescription = string.Empty;
<br/>
} <br/>
virtual public string Name {
<br/>
get { return myName; }
<br/>
set { myName = value; }
<br/>
} <br/>
virtual public string Description {
<br/>
get { return myDescription; }
<br/>
set { myDescription = value; }
<br/>
} <br/>
public MyElement Parent {
<br/>
get { return myParent; }
<br/>
set { myParent = value; }
<br/>
}<br/>
public void CancelEdit() {<br/>
// code to cancel edit<br/>
}<br/>
public void EndEdit() {<br/>
// code to end edit<br/>
}<br/>
public void BeginEdit() {<br/>
// code to begin edit<br/>
}<br/>
}
public class MyElementList : List<MyElement> { <br/>
public MyElementList() { <br/>
} <br/>
}
public class MyField : MyElementAdapter { <br/>
private int size;<br/>
public MyField(MyElement parent) : base(parent) {
<br/>
size = 1; <br/>
}<br/>
public string MyFieldName {
<br/>
get { return this.Name; }
<br/>
set { this.Name = value; }
<br/>
} <br/>
public string MyFieldDescription {
<br/>
get { return this.Description; }
<br/>
set { this.Description = value;}
<br/>
} <br/>
public int Size {
<br/>
get { return size; }
<br/>
set { size = value; }
<br/>
} <br/>
}
public class MyRecordOfFields : MyElementAdapter { <br/>
private MyElementList myMembers; <br/>
public MyRecordOfFields(MyElement parent) : base(parent) {
<br/>
myMembers = new MyElementList();
<br/>
} <br/>
public MyElementList MyMembers {
<br/>
get { return myMembers; }
<br/>
set { myMembers = value; }
<br/>
} <br/>
}
<br/>
My project contains the following:<br/>
- MyElement is an interface;<br/>
- MyElementAdapter is an abstract derived from MyElement; <br/>
- MyElementList is a generic list of MyElement;<br/>
- MyField is derived from MyElementAdapter.
When the AddItem_Click method is done, I see that my underlying data (myLocalField.MyMembers) changes, but bindingSource1 and dataGridView1 do not. In the past, any updates were done to the underlying data as well as the bindingsource - I would like to make
changes to the underlying data and have that propogate to the bindingsource/datagridview.

I figured the method ResetBindings(true) would do refresh/reset the datagridview - it does not. Is there another method that I need to use in order for me to update the datagridview without adding additional datagridview events?
Any assistance is appreciated. Thank you.<br/>
- Lorentz

View the full article
 
Back
Top