I am creating a hierarchy grid using object relational data. I am adding a GridViewRelation manually to link the two classes together. The data loads correctly at first, but then selecting a child row will cause all child rows in that position to be duplicated and selected.
After initial data load, everything looks great.
(See dataload.png)
Select Child 2_1 and all first position children are selected.
(see select1.png)
Select Child 3_2 and now all second position children are selected, and the first position children are now duplicates of each other and read Child 4_1.
(see select2.png)
Here is the code:
namespace TestGrid
{
public partial class Form1 : Form
{
private BindingList<
MasterObject
> data;
public Form1()
{
InitializeComponent();
SetupGrid();
}
private void SetupGrid()
{
LoadMasterTemplate(grid.MasterTemplate);
GridViewTemplate childTemplate = new GridViewTemplate();
LoadChildTemplate(childTemplate);
grid.MasterTemplate.Templates.Add(childTemplate);
GridViewRelation relation = new GridViewRelation(grid.MasterTemplate, childTemplate);
relation.RelationName = "Children";
relation.ChildColumnNames.Add("Children");
grid.Relations.Add(relation);
}
private void LoadMasterTemplate(GridViewTemplate template)
{
template.Columns.Clear();
template.EnableFiltering = false;
template.AllowAddNewRow = false;
template.ShowChildViewCaptions = false;
template.AutoGenerateColumns = false;
template.EnableGrouping = false;
GridViewTextBoxColumn colNode = new GridViewTextBoxColumn("NodeDesc");
colNode.HeaderText = "Node";
colNode.Name = "Node";
colNode.ReadOnly = true;
template.Columns.Add(colNode);
GridViewTextBoxColumn colName = new GridViewTextBoxColumn("Name");
colName.HeaderText = "Name";
colName.Name = "Name";
template.Columns.Add(colName);
template.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
}
private void LoadChildTemplate(GridViewTemplate template)
{
template.Columns.Clear();
template.EnableFiltering = false;
template.EnableGrouping = false;
template.AllowAddNewRow = false;
template.AutoGenerateColumns = false;
GridViewTextBoxColumn colNode = new GridViewTextBoxColumn("NodeDesc");
colNode.HeaderText = "Node";
colNode.Name = "Node";
colNode.ReadOnly = true;
template.Columns.Add(colNode);
GridViewTextBoxColumn colIP = new GridViewTextBoxColumn("Name");
colIP.Name = "Name";
colIP.HeaderText = "Name";
template.Columns.Add(colIP);
template.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
}
private void LoadData()
{
data = new BindingList<
MasterObject
>();
for (int i = 1; i <
5
; i++)
{
MasterObject
m
=
new
MasterObject();
m.Name
=
"Master "
+ i;
for (int
x
=
1
; x < i; x++)
{
ChildObject
c
=
new
ChildObject();
c.Name
=
"Child "
+ i + "_" + x;
m.Children.Add(c);
}
data.Add(m);
}
grid.DataSource
=
data
;
}
private void button1_Click(object sender, EventArgs e)
{
// Load Data In Grid
LoadData();
}
private void button2_Click(object sender, EventArgs e)
{
// Add Child to 1
MasterObject
m
=
data
[0];
ChildObject
c
=
new
ChildObject();
c.Name
=
"Child "
+ "1_" + (m.Children.Count + 1);
m.Children.Add(c);
}
private void button3_Click(object sender, EventArgs e)
{
// Add Child to 2
MasterObject
m
=
data
[1];
ChildObject
c
=
new
ChildObject();
c.Name
=
"Child "
+ "2_" + (m.Children.Count + 1);
m.Children.Add(c);
}
}
public class MasterObject
{
private string name;
public string Name
{
get
{
return name;
}
set
{
if (name != value)
{
name
=
value
;
}
}
}
public string NodeDesc
{
get
{
return "MasterObject";
}
}
private BindingList<ChildObject> children;
public BindingList<
ChildObject
> Children
{
get
{
return children;
}
set
{
children = value;
}
}
public MasterObject()
{
children = new BindingList<
ChildObject
>();
}
}
public class ChildObject
{
private string name;
public string Name
{
get
{
return name;
}
set
{
if (name != value)
{
name = value;
}
}
}
public string NodeDesc
{
get
{
return "ChildObject";
}
}
public ChildObject() { }
}
}