前面我有给NickLee的一个控件地址,不过在线学习没有demo我们很多时候很难下手,我做了个简单的demo,是关于父子列表清单,我们在很多时候做erp会遇见bom清单,也就是一个订单对应哪些产品,而一种产品对应哪些零部件,也就是一个产品它会有哪些材料清单。这样就会出现一个层层展开的关系,而且在设计权限的时候我们也会遇见父子权限或者上下级权限清单,在以前有用过gridview嵌套,后来找到这个控件之后就没有使用了,可以直接实现,不知道大家有没有更好的类似的控件,希望能够介绍。
cs代码:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using NickLee.Web.UI;
using System.Collections.Generic;
using System.Reflection;
namespace NPOI
{
public partial class _Default : System.Web.UI.Page
{
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
Grid1.NeedRebind += new Grid.NeedRebindEventHandler(OnNeedRebind);
Grid1.NeedDataSource += new Grid.NeedDataSourceEventHandler(OnNeedDataSource);
//加载子列表清单事件
Grid1.NeedChildDataSource += new Grid.NeedChildDataSourceEventHandler(OnNeedChildData);
Grid1.PageIndexChanged += new Grid.PageIndexChangedEventHandler(OnPageChanged);
Grid1.SortCommand += new Grid.SortCommandEventHandler(OnSort);
}
//override protected void OnInit(EventArgs e)
//{
// //
// // CODEGEN: This call is required by the ASP.NET Web Form Designer.
// //
// InitializeComponent();
// base.OnInit(e);
//}
///// <summary>
///// Required method for Designer support - do not modify
///// the contents of this method with the code editor.
///// </summary>
//private void InitializeComponent()
//{
// //this.Load += new System.EventHandler(this.Page_Load);
//}
#endregion
public static List<Model> ListTable=null;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
IList<Model> model = new List<Model>{
new Model{ParentId="001000",ChildId="001000",UserName="Bill",Department="IT",IsWork=1},
new Model{ParentId="001000",ChildId="001001",UserName="Bil2",Department="IT",IsWork=1},
new Model{ParentId="001000",ChildId="001002",UserName="Bil3",Department="IT",IsWork=1},
new Model{ParentId="001002",ChildId="001007",UserName="Bil4",Department="IT",IsWork=1},
new Model{ParentId="002000",ChildId="002000",UserName="Bil5",Department="PUR",IsWork=1},
new Model{ParentId="002000",ChildId="002001",UserName="Bil6",Department="PUR",IsWork=1},
new Model{ParentId="002001",ChildId="002002",UserName="Bil7",Department="PUR",IsWork=0},
new Model{ParentId="002002",ChildId="002003",UserName="Bil8",Department="PUR",IsWork=1},
new Model{ParentId="002002",ChildId="002004",UserName="Bil9",Department="PUR",IsWork=1},
new Model{ParentId="002002",ChildId="002005",UserName="Bila",Department="PUR",IsWork=1},
new Model{ParentId="003000",ChildId="003000",UserName="Bilc",Department="PE",IsWork=1},
new Model{ParentId="004000",ChildId="004000",UserName="Bilb",Department="ME",IsWork=1},
new Model{ParentId="004000",ChildId="004001",UserName="Bild",Department="ME",IsWork=0},
new Model{ParentId="004000",ChildId="004002",UserName="Bile",Department="ME",IsWork=1},
new Model{ParentId="004000",ChildId="004003",UserName="Bilf",Department="ME",IsWork=1}
};
ListTable = model as List<Model>;
buildTopLevel();
Grid1.DataBind();
}
}
private void buildTopLevel()
{
var GetDate = from row in ListTable
where row.ParentId == row.ChildId
select new { row.ChildId, row.UserName, row.Department, row.IsWork };
DataTable table = new DataTable("Employee");
table=ListToDataTable(GetDate);
Grid1.DataSource = table;
}
public void OnNeedRebind(object sender, EventArgs oArgs)
{
Grid1.DataBind();
}
public void OnNeedDataSource(object sender, EventArgs oArgs)
{
buildTopLevel();
}
//获得子列表
public void OnNeedChildData(object sender, NickLee.Web.UI.GridNeedChildDataSourceEventArgs args)
{
var GetChildData = from row in ListTable
where row.ParentId == args.Item["ChildId"].ToString() && row.ChildId != args.Item["ChildId"].ToString()
select new { row.ChildId, row.UserName, row.Department, row.IsWork };
DataTable oTable=new DataTable("Employee");
oTable = ListToDataTable(GetChildData);
args.DataSource = oTable;
}
public void OnPageChanged(object sender, NickLee.Web.UI.GridPageIndexChangedEventArgs oArgs)
{
Grid1.CurrentPageIndex = oArgs.NewIndex;
}
public void OnSort(object sender, NickLee.Web.UI.GridSortCommandEventArgs oArgs)
{
Grid1.Sort = oArgs.SortExpression;
}
public static DataTable ListToDataTable<T>(IEnumerable<T> data)
{
PropertyInfo[] ps = typeof(T).GetProperties();
DataTable table = new DataTable();
foreach (PropertyInfo p in ps)
{
table.Columns.Add(p.Name, p.PropertyType);
}
foreach (T t in data)
{
DataRow row = table.NewRow();
foreach (PropertyInfo p in ps)
{
row[p.Name] = p.GetValue(t, null);
}
table.Rows.Add(row);
}
return table;
}
}
public class Model
{
public string ParentId { get; set; }
public string ChildId { get; set; }
public string UserName { get; set; }
public string Department { get; set; }
public int IsWork { get; set; }
}
}
aspx代码:
代码
1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="NPOI._Default" %>
2 <%@ Register TagPrefix="ComponentArt" Namespace="NickLee.Web.UI" Assembly="NickLee.Web.UI" %>
3
4 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
6 <html xmlns="http://www.w3.org/1999/xhtml" >
7 <head runat="server">
8 <link href="CSS/gridStyle.css" type="text/css" rel="Stylesheet"/>
9 <link href="common/css/demos.css" type="text/css" rel="Stylesheet" />
10 <title>未命名頁面</title>
11 </head>
12 <body>
13 <form id="form1" runat="server">
14
15 <div class="DemoArea">
16
17 <ComponentArt:Grid id="Grid1"
18 PreloadLevels="false"
19 SelfReferencing="true"
20 RunningMode="Callback"
21 CssClass="GridClass"
22 ShowFooter="false"
23 IndentCellWidth="19"
24 IndentCellCssClass="IndentCell"
25 TreeLineImageWidth="19"
26 TreeLineImageHeight="20"
27 PageSize="15"
28 ImagesBaseUrl="images/"
29 TreeLineImagesFolderUrl="images/lines/"
30 Width="700"
31 runat="server" >
32 <Levels>
33 <ComponentArt:GridLevel
34 DataMember="Employee"
35 DataKeyField="ChildId"
36 SelectedRowCssClass="SelectedRowClass"
37 HeadingTextCssClass="HeadingTextClass"
38 HeadingCellCssClass="HeadingCellClass"
39 AlternatingRowCssClass="AlternatingRowClass"
40 RowCssClass="RowClass"
41 HeadingRowCssClass="HeadingRowClass"
42 TableHeadingCssClass="TableHeading"
43 GroupHeadingCssClass="TableHeading"
44 SortDescendingImageUrl="desc.gif"
45 SortAscendingImageUrl="asc.gif"
46 SortImageHeight="5"
47 SortImageWidth="9"
48 SelectorCellCssClass="SelectorCell"
49 DataCellCssClass="DataCell"
50 SelectorImageUrl="selector.gif"
51 SelectorCellWidth="19"
52 ShowSelectorCells="true">
53 <Columns>
54 <ComponentArt:GridColumn DataField="ChildId" HeadingText="EmployeeId" />
55 <ComponentArt:GridColumn DataField="UserName" HeadingText="EmployeeName" />
56 <ComponentArt:GridColumn DataField="Department" HeadingText="EmployeeDepartment" />
57 <ComponentArt:GridColumn DataField="IsWork" HeadingText="WhetherAtWork" />
58 </Columns>
59 </ComponentArt:GridLevel>
60 </Levels>
61 </ComponentArt:Grid>
62 </div>
63
64 </form>
65 </body>
66 </html>
67
最后产生的结果是:
demo下载地址
NicKLee的demo下载地址