I have an application where I am displaying a List<> object in a DataGridView.
If I add an item to the list programmatically, I can edit that item in the DataGridView just fine, and any changes are reflected in the databound object.
However, if I have the DataGridView's AllowUserToAddRows property set to True, I can add rows in the DataGridView, but these items are not added to my underlying list. The weird thing is, though, that I can delete an item view the DataGridView, and it is removed from the list.
Here is my List<> descendant class:
class
Global
{
private
string
_Label = String.Empty;
private
string
_Value = String.Empty;
public
string
Label
{
get
{ return
_Label; }
set
{ _Label = value; }
}
public
string
Value
{
get
{ return
_Value; }
set
{ _Value = value; }
}
public
string
Output
{
get
{ return
String.Format("[{0}]={1}"
, _Label, _Value); }
}
public
Global(string
label, string
value)
{
_Label = label;
_Value = value;
}
}
class
Globals : List<Global>
{
public
XElement Xml
{
get
{
XElement thisElement = new
XElement("Globals"
);
foreach
(Global thisGlobal in
this
)
{
thisElement.Add(new
XElement("Global"
, thisGlobal.Output));
}
return
thisElement;
}
}
}
And here is my binding code:
bindingSourceGlobals.DataSource = null
;
dataGridViewGlobals.DataSource = null
;
_Globals.Add(new
Global("New Label"
, "New Value"
));
bindingSourceGlobals.DataSource = _Globals;
dataGridViewGlobals.DataSource = bindingSourceGlobals;
How can I force additions in the DataGridView to reflect in the underlying List<> object?
Thanks!
Gabe