前面那个有问题
public partial class Form1 : Form
{
public Form1() {
InitializeComponent();
grid.Tree.Column = 0;
grid.Rows.Count = 1;
grid.Rows[0][0] = "会计科目";
grid.Rows[0][1] = "期初金额";
grid.Cols[0].Width = 250;
ZwDataContext zw = new ZwDataContext();
kmALL = zw.diKm.Where(r => r.KjYear == 2013).OrderBy(r => r.Kmdm).ToList();
//grid.DataSource = new BindingSource(list, ""); // 使用绑定数据源时
AddCategory("1", "资产类");
AddCategory("2", "负债类");
AddCategory("3", "所有者权益类");
AddCategory("4", "成本类");
AddCategory("5", "损益类");
}
private int[] KmLen = { 3, 5, 8 }; // 各级科目宽度
private List<diKm> kmALL; // 科目列表
private void AddCategory(string category, string desc) {
Row row = grid.AddItem(desc);
row.AllowEditing = false;
row.IsNode = true;
var q = kmALL.Where(r => r.Kmdm.Substring(0, 1) == category && r.Kmdm.Length == KmLen[0]);
q.ToList().ForEach(r => AddChildsNode(r, row.Node));
}
private void AddChildsNode(diKm km, Node parent) {
Bitmap bp = Resources.Km;
bp.MakeTransparent(Color.Fuchsia);
Node node = parent.AddNode(NodeTypeEnum.LastChild, km.Kmdm + " " + km.Kmmc, km, bp);
int level = Array.IndexOf(KmLen, km.Kmdm.Length);
if (level + 1 < KmLen.Length) {
var childs = kmALL.Where(r => r.Kmdm.Length == KmLen[level + 1] &&
r.Kmdm.Substring(0, km.Kmdm.Length) == km.Kmdm).ToList();
childs.ForEach(r => AddChildsNode(r, node));
}
}
}
有个问题,就是如何将Node.Row绑定到数据源, 如果先将grid绑定到数据源,那设置Row.IsNode会报错。查过文档,这个节点属性不支持绑字数据行,有什么办法吗? |