我们使用表格主要就是为了让表格绑定上常规的二维表数据,并进行增、删、改、查,最后再进行保存。而加载二维表的方式有很多,下面我们就通过代码示例进行详细介绍。
方法名称 | 分类 | 描述 |
BuildClassTree | 数据加载 | 根据从指定程序集中查找指定类型的类,并根据命名空间创建成目录树. |
BuildTree | 数据加载 | 根据Datatable及对应的字段生成目录树。 |
Fill(TreeViewFilterClient, Boolean, Boolean) | 数据加载 | 依据TreeViewFilterClient对象加载拥有父子键的无限目录树 |
ReBuildTree | 数据加载 | 刷新目录树。仅适合拥有父子键的无限目录树。 |
ReBuildTreeByDataTableHelp | 数据加载 | 这里仅适合父子目录树场景,此示例仅适合那些已经加载过数据,然后需要重新加载表数据并刷新显示的场景。 |
主要通过三种方式,一种是通过Fill方法,这种方式是针对拥有父子键的无限目录树。由于子键具有唯一性,所以具备可编辑、保存的功能。同时,目录树的加载同时支持挖掘模式。第二种是通过BuildClassTree方法,这种方式主要是加载展示一些dll中的一些类展示,并不具备编辑、保存的功能。第三种是通过BuildTree方法,将现有DataTableHelp中的其中几列以目录树的形式加载出来,方便做一些加载、筛选树,这种也不适合编辑、保存。
Fill方法的语法:
Fill(treeViewFilter,isViewModel,fillData)
参数名称 | 说明 |
treeViewFilter | 必填参数,TreeViewFilterClient类型,设置加载目录树的数据规范,父子目录树主要就依靠这些参数实现加载。 |
isViewModel | 可选参数,逻辑型,是否只读模式。如果想编辑保存表中的数据,请设置此参数为False。默认值为True。 |
fillData | 可选参数,逻辑型,表示是否填充表数据。默认值为True。 |
示例:我们可以设计一个类似下面的窗体,用来测试多种加载数据的方式。
Vb.Net |
Imports System Imports System.Text Imports System.IO Imports System.Data Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Drawing.Imaging Imports System.Drawing.Text Imports System.Diagnostics Imports System.Collections Imports System.Collections.Generic Imports System.Runtime.InteropServices Imports System.Collections.Specialized Imports System.Windows.Forms Imports System.Reflection Imports Microsoft.CSharp Imports Microsoft.VisualBasic Imports sanMuSoft.Utility Imports sanMuSoft.Data.Pivot Imports System.Linq Imports System.Threading Imports System.Threading.Tasks Imports System.Xml Imports System.Data.Common Imports System.Net.Http Imports C1.C1Excel Imports C1.C1Zip Imports C1.Win.C1Command Imports C1.Win.C1FlexGrid Imports C1.Win.C1Input Imports C1.Win.C1Ribbon Imports C1.Win.C1Themes Imports sanMuSoft.CS.Framework Imports sanMuSoft.CS.Framework.Editor Imports sanMuSoft.CS.Framework.FormDesigner Imports sanMuSoft.CS.Framework.DropDownForms Imports sanMuSoft.CS.WinForm Imports sanMuSoft.CS.WinForm.Editor Imports sanMuSoft.CS.WinForm.Controls Imports sanMuSoft.CS.WinForm.Controls.Grid Imports sanMuSoft.CS.WinForm.Controls.BoxControls Imports sanMuSoft.CS.Workflow Imports sanMuSoft.CS.Report Imports sanMuSoft.Data Imports sanMuSoft.Data.TableBuilder Imports sanMuSoft.CS.ShareFunc Imports Newtonsoft.Json.Linq Imports Newtonsoft.Json Imports Aliyun.OSS Namespace FormEvents Public Class Formbdf887d00105456c97b0399a0b07fa0f Inherits FormEventsBase Private tblTree As SmEditTreeGrid Public Sub SmEditTreeGrid数据加载_Load(sender As Object,e As System.EventArgs) tblTree=Me.SmForm.ControlDictionary()("tblTree") End Sub Public Sub btnFill_Click(sender As Object,e As System.EventArgs) '定义一个TreeViewFilterClient对象,用来加载 Dim treeFill As TreeViewFilterClient = New TreeViewFilterClient() treeFill.FiledFatherID = "FatherRoleID" treeFill.FieldID = "RoleID" treeFill.FieldNodeName = "RoleName" treeFill.FieldIndex = "FIndex" treeFill.FieldKeyIsGUID = True treeFill.Sort = "FIndex" treeFill.ConnectionName = Proj.DatabaseForSystem.Name treeFill.Database = Proj.DatabaseForSystem treeFill.SQLString = "select * from SysRightRoles" tblTree.Fill(treeFill, False) End Sub Public Sub btnFillSimple_Click(sender As Object,e As System.EventArgs) '可以通过目录树选择数据规范RuleID来加载TreeViewFilterClient对象 Dim treeclient As TreeViewFilterClient = New TreeViewFilterClient("143aa98654404753a0540cb5c436e88d", True) tblTree.Fill(treeclient, True) End Sub Public Sub btnBuildClassTree_Click(sender As Object,e As System.EventArgs) '先设定要从哪个dll里面查找类型 Dim strAssemblyPath As String="sanMuSoft.CS.Framework.dll" 'Path.Combine(Proj.ApplicationPath,"sanMuSoft.CS.Framework.dll") '加载所有实现Form的类 tblTree.BuildClassTree(strAssemblyPath,GetType(Form)) End Sub Public Sub btnBuildTree_Click(sender As Object,e As System.EventArgs) '先加载一个表,或者从哪个窗体中获取一个表 Dim dt As DataTableHelp=Proj.SysDataFactory("UserDB").ExecuteDataTableHelp("select * from 订单信息") '将表中几个字段按目录树的形式加载 tblTree.BuildTree(dt,"产品,客户,业务员") End Sub Public Sub btnRefresh_Click(sender As Object,e As System.EventArgs) '只有父子目录树适合重新刷新加载 tblTree.ReBuildTree() End Sub Public Sub btnReload_Click(sender As Object,e As System.EventArgs) '这里仅适合父子目录树场景,此示例仅适合那些已经加载过数据,然后需要重新加载表数据并刷新显示的 '方法一:自己重新加载数据,然后刷新表 tblTree.DataTableHelp.LoadFilter="ID='1234'" '重新加载数据 tblTree.DataTableHelp.Load(False) tblTree.ReBuildTreeByDataTableHelp(tblTree.DataTableHelp) '方法二:设置重新加载条件,然后刷新 tblTree.DataTableHelp.LoadFilter="ID='1234'" tblTree.ReBuildTree() End Sub End Class End Namespace |
C# |
using System; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.IO; using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; using System.Security; using System.Text; using System.Threading.Tasks; using Microsoft.VisualBasic; using System.Data; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.Drawing.Text; using System.Collections; using System.Runtime.InteropServices; using System.Collections.Specialized; using System.Windows.Forms; using Microsoft.CSharp; using sanMuSoft.Utility; using sanMuSoft.Data.Pivot; using System.Threading; using System.Xml; using System.Data.Common; using System.Net.Http; using C1.C1Excel; using C1.C1Zip; using C1.Win.C1Command; using C1.Win.C1FlexGrid; using C1.Win.C1Input; using C1.Win.C1Ribbon; using C1.Win.C1Themes; using sanMuSoft.CS.Framework; using sanMuSoft.CS.Framework.Editor; using sanMuSoft.CS.Framework.FormDesigner; using sanMuSoft.CS.Framework.DropDownForms; using sanMuSoft.CS.WinForm; using sanMuSoft.CS.WinForm.Editor; using sanMuSoft.CS.WinForm.Controls; using sanMuSoft.CS.WinForm.Controls.Grid; using sanMuSoft.CS.WinForm.Controls.BoxControls; using sanMuSoft.CS.Workflow; using sanMuSoft.CS.Report; using sanMuSoft.Data; using sanMuSoft.Data.TableBuilder; using sanMuSoft.CS.ShareFunc; using Newtonsoft.Json.Linq; using Newtonsoft.Json; using Aliyun.OSS; namespace FormEvents { public class Formbdf887d00105456c97b0399a0b07fa0f : FormEventsBase { private SmEditTreeGrid tblTree; public void SmEditTreeGrid数据加载_Load(object sender, System.EventArgs e) { tblTree = this.SmForm.ControlDictionary()["tblTree"] as SmEditTreeGrid; } public void btnFill_Click(object sender, System.EventArgs e) { // 定义一个TreeViewFilterClient对象,用来加载 TreeViewFilterClient treeFill = new TreeViewFilterClient(); treeFill.FiledFatherID = "FatherRoleID"; treeFill.FieldID = "RoleID"; treeFill.FieldNodeName = "RoleName"; treeFill.FieldIndex = "FIndex"; treeFill.FieldKeyIsGUID = true; treeFill.Sort = "FIndex"; treeFill.ConnectionName = Proj.DatabaseForSystem.Name; treeFill.Database = Proj.DatabaseForSystem; treeFill.SQLString = "select * from SysRightRoles"; tblTree.Fill(treeFill, false); } public void btnFillSimple_Click(object sender, System.EventArgs e) { // 可以通过目录树选择数据规范RuleID来加载TreeViewFilterClient对象 TreeViewFilterClient treeclient = new TreeViewFilterClient("143aa98654404753a0540cb5c436e88d", true); tblTree.Fill(treeclient, true); } public void btnBuildClassTree_Click(object sender, System.EventArgs e) { // 先设定要从哪个dll里面查找类型 string strAssemblyPath = "sanMuSoft.CS.Framework.dll"; // Path.Combine(Proj.ApplicationPath,"sanMuSoft.CS.Framework.dll") // 加载所有实现Form的类 tblTree.BuildClassTree(strAssemblyPath, typeof(Form)); } public void btnBuildTree_Click(object sender, System.EventArgs e) { // 先加载一个表,或者从哪个窗体中获取一个表 DataTableHelp dt = Proj.SysDataFactory["UserDB"].ExecuteDataTableHelp("select * from 订单信息"); // 将表中几个字段按目录树的形式加载 tblTree.BuildTree(dt, "产品,客户,业务员"); } public void btnRefresh_Click(object sender, System.EventArgs e) { // 只有父子目录树适合重新刷新加载 tblTree.ReBuildTree(); } public void btnReload_Click(object sender, System.EventArgs e) { // 这里仅适合父子目录树场景,此示例仅适合那些已经加载过数据,然后需要重新加载表数据并刷新显示的 // 方法一:自己重新加载数据,然后刷新表 tblTree.DataTableHelp.LoadFilter = "ID='1234'"; // 重新加载数据 tblTree.DataTableHelp.Load(false); tblTree.ReBuildTreeByDataTableHelp(tblTree.DataTableHelp); // 方法二:设置重新加载条件,然后刷新 tblTree.DataTableHelp.LoadFilter = "ID='1234'"; tblTree.ReBuildTree(); } } } |
1.btnFill_Click执行结果
2.btnFillSimple_Click执行结果
3.btnBuildClassTree_Click执行结果
4.btnBuildTree_Click执行结果