I am binding objects to a hierarchy grid in code and when a change is made a child row the change is shown in the grid, however, the underlying data object is not updated. I can manually apply the new value after edit to the object, but it seems like grid should be doing this for me.
public
partial
class
Form1 : Form
{
private
BindingList<Node> AllNodes =
new
BindingList<Node>();
public
Form1()
{
InitializeComponent();
}
private
void
Form1_Load(
object
sender, EventArgs e)
{
LoadData();
}
public
void
LoadData()
{
LoadAllNodes();
grid.DataSource = AllNodes;
LoadAllObjectColumns(grid.MasterTemplate);
// Load child template
GridViewTemplate template =
new
GridViewTemplate();
LoadAllObjectColumns(template);
grid.MasterTemplate.Templates.Add(template);
GridViewRelation r =
new
GridViewRelation(grid.MasterTemplate, template);
r.ChildColumnNames.Add(
"Children"
);
grid.Relations.Add(r);
}
public
void
LoadAllNodes()
{
Node p1 =
new
Node(
"Parent 1"
, ItemStatus.None);
Node n1 =
new
Node(
"Child 1"
, ItemStatus.None);
Node n2 =
new
Node(
"Child 2"
, ItemStatus.None);
p1.Children.Add(n1);
p1.Children.Add(n2);
AllNodes.Add(p1);
Node p2 =
new
Node(
"Parent 2"
, ItemStatus.None);
AllNodes.Add(p2);
}
public
static
void
LoadAllObjectColumns(GridViewTemplate template)
{
template.Columns.Clear();
template.EnableFiltering =
true
;
template.AllowAddNewRow =
false
;
template.AutoGenerateColumns =
false
;
GridViewTextBoxColumn colName =
new
GridViewTextBoxColumn(
"Name"
);
colName.HeaderText =
"Name"
;
colName.Name =
"Name"
;
template.Columns.Add(colName);
GridViewComboBoxColumn colStatus =
new
GridViewComboBoxColumn(
"Status"
);
colStatus.HeaderText =
"Status"
;
colStatus.Name =
"Status"
;
colStatus.DataSource = Enum.GetValues(
typeof
(ItemStatus)).Cast<ItemStatus>();
template.Columns.Add(colStatus);
template.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
}
public
enum
ItemStatus { None, New, Test, Complete };
public
class
Node : INotifyPropertyChanged
{
public
event
PropertyChangedEventHandler PropertyChanged;
protected
void
NotifyPropertyChanged(String info)
{
if
(PropertyChanged !=
null
)
{
PropertyChanged(
this
,
new
PropertyChangedEventArgs(info));
}
}
private
string
name;
private
ItemStatus status;
private
BindingList<Node> children;
public
string
Name
{
get
{
return
name;
}
set
{
if
(name != value)
{
name = value;
NotifyPropertyChanged(
"Name"
);
}
}
}
public
ItemStatus Status
{
get
{
return
status;
}
set
{
if
(status != value)
{
status = value;
NotifyPropertyChanged(
"Status"
);
}
}
}
public
BindingList<Node> Children
{
get
{
return
children;
}
set
{
children = value;
}
}
public
Node(
string
name, ItemStatus status)
{
this
.name = name;
this
.status = status;
children =
new
BindingList<Node>();
}
}
}