首 页 网络编程
网页制作 图形图象 操作系统 冲浪宝典
软件教学 认证考试

网络安全 网络办公 行业资讯 评测对比
您当前位置:站长天空 -> 网络编程-> C#/CSHARP教程
datatree和数据库绑定的最少代码_delphi教程
作者:网友供稿 点击:0
推荐
西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!可在线rar解压,自动数据恢复设置虚拟目录等.免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金
站内搜索
文章页数:[1] 

unit mmslibrarypage;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ImgList, VirtualTrees, ComCtrls, ToolWin, mpToolBar,basicModal,DataModal,
  database,myScroll,insertdemon,mysql,newmmsForlibrary;

type
  TMMSLibraryForm = class(TPageForm)
    mainToolBar: TmpToolBar;
    btnNewMMS: TToolButton;
    btnDelete: TToolButton;
    DataTree: TVirtualStringTree;
    ilImages: TImageList;
    btnImport: TToolButton;
    btn2: TToolButton;
    btn3: TToolButton;
    btn4: TToolButton;
    btn5: TToolButton;
    btnExport: TToolButton;
    btnRefresh: TToolButton;
    procedure FormCreate(Sender: TObject);
    procedure btnNewMMSClick(Sender: TObject);
    procedure btnDeleteClick(Sender: TObject);
    procedure btnImportClick(Sender: TObject);
    procedure btnExportClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure btnRefreshClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure DataTreeGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
      Column: TColumnIndex; TextType: TVSTTextType;
      var CellText: WideString);
    procedure DataTreeInitNode(Sender: TBaseVirtualTree; ParentNode,
      Node: PVirtualNode; var InitialStates: TVirtualNodeInitStates);
  
  private
    { Private declarations }
    stringlist:TStringList;
  public
    procedure  refreshtree();
  end;

  procedure GetMMSFromLibrary(dparam: TStringList; mysql_rows: PMYSQL_ROW);

type
 PMMSTreeNode=^TMMStreeNode;

//自定义的datatree的结构
 TMMStreeNode=record
   mmsid:string;
   mmstype:string;
   mmssmil:string;
   mmssize:Integer;
   mmssubject:string;
 end;

var
  MMSLibraryForm: TMMSLibraryForm;

implementation

{$R *.dfm}

procedure TMMSLibraryForm.FormCreate(Sender: TObject);
begin
  Self.Font:=Application.MainForm.Font;                                        //保持程序中字体一致
  stringlist:=TStringList.Create;                                                       //保存要显示的数据
  DataTree.NodeDataSize:=SizeOf(TMMStreeNode);                 //初始化datatree的节点大小
end;

procedure TMMSLibraryForm.FormShow(Sender: TObject);
var
  pdbinfo: PDatabaseInfo;
  column : TVirtualTreeColumn;
  Header : TStrings;
  i      : Integer;
  node   : PVirtualNode;
  data   : PINT;

  sql    : string;
  pnode1 : PMMSTreeNode;
begin
  Header:=TStringList.Create;

//添加显示的列名
  if trim(Header.Text)= then
  begin
    Header.Add(ID);
    Header.Add(Type);
    Header.Add(Smil 1.0/2.0);
    Header.Add(Size(B));
    Header.Add(Subject);
  end;

//设置列的显示

  for i:=0 to Header.Count-1 do
  begin
    column:= DataTree.Header.Columns.Add;
    column.Text:= Header[I];
    column.Width:=dataTree.ClientWidth div 6;
    if i=4 then
       column.Width:= dataTree.ClientWidth div 3-10;
  end;

//    连接数据库,获取指定的数据库(是不是忒简单

pdbinfo:=currentdatabase.databases.GetByIndex(0);

    Refreshtree;

end;

procedure  TMMSLibraryForm.refreshtree();
var
  sql : string;
begin
  DataTree.Clear;
  stringlist.Clear;

  sql:=select MMS_ID,MMS_Type,MMS_Smil,MMS_Size,MMS_Subject from mmslibrary;

//回调函数:让被调用者调用调用者自身的函数。执行ExeuteSQlQurey时调用了GetMMSFromLibrary

(***********************************************************************************************************

procedure TDatabase.ExeuteSQlQurey(dparam: Pointer; sql: string; callback: TDbDataCallBack);
var
  aHandle, db: pmysql;
  qresult: PMYSQL_RES;
  mysql_rows: PMYSQL_ROW;
  iRtn, fcount, i: Integer;
begin

  try
    aHandle := mysql_init(nil);
   //mysql_real_connect(aHandle, nil, nil, nil, nil, 0, nil, 0);
    if commonconfig.remotemode = 0 then
      db := mysql_real_connect(aHandle, nil, nil, nil, PAnsichar(FCurrentDataBase), 0, nil, 0)
    else
      db := mysql_real_connect(aHandle, PAnsichar(RHost), PAnsichar(RUser), PAnsichar(RPassword), PAnsichar(FCurrentDataBase), 0, nil, 0);

    if db <> nil then
    begin
      iRtn := mysql.mysql_query(db, Pchar(sql));
      if iRtn = 0 then
      begin
        qresult := mysql.mysql_store_result(db);
        if qresult <> nil then
        begin
          fcount := mysql.mysql_num_rows(qresult);
          for i := 0 to fcount - 1 do
          begin
            mysql_rows := mysql.mysql_fetch_row(qresult);
            callback(dparam, mysql_rows);
          end;
        end;
        mysql.mysql_free_result(qresult);
      end;
    end;
  finally
    mysql_Close(db);
  end;

end;

*************************************************************************************************************)


  currentdatabase.ExeuteSQlQurey(stringlist,sql,@GetMMSFromLibrary);

  DataTree.RootNodeCount:=stringlist.Count div 5 ; //小技巧啦
  DataTree.Refresh;
end;

//daparam即是currentdatabase.ExeuteSQlQurey(stringlist,sql,@GetMMSFromLibrary);中的stringlist,

//即用被调用者的值(mysql_rows )初始化调用者的参数

//这个观点很重要,使用也很广泛和特殊
procedure GetMMSFromLibrary(dparam:  TStringList; mysql_rows: PMYSQL_ROW);
begin
    dparam.Add(mysql_rows[0]);
    dparam.Add(mysql_rows[1]);
    dparam.Add(mysql_rows[2]);
    dparam.Add(mysql_rows[3]);
    dparam.Add(mysql_rows[4]);
end;

procedure TMMSLibraryForm.btnNewMMSClick(Sender: TObject);
begin
  NewMMS:=TNewMMS.Create(MMSLibraryForm);
  if NewMMS.ShowModal= mrok then
     refreshtree;
end;

procedure TMMSLibraryForm.btnDeleteClick(Sender: TObject);
var
  node:PVirtualNode;

  sql:string;
begin
  if DataTree.FocusedNode=nil then  Exit;

  node:=DataTree.FocusedNode;

  sql:=delete from mmslibrary where MMS_ID="+ stringlist[node.index * 5]+";
  currentdatabase.ExecuteSqlNoQurey(sql);
  RefreshTree;

end;
procedure TMMSLibraryForm.btnImportClick(Sender: TObject);
var
  dl:TOpenDialog;
begin
  dl.Filter:=eml files|*.eml;

end;

procedure TMMSLibraryForm.btnExportClick(Sender: TObject);
begin
//
end;

procedure TMMSLibraryForm.btnRefreshClick(Sender: TObject);
begin
  RefreshTree;
end;

procedure TMMSLibraryForm.FormDestroy(Sender: TObject);
begin
  stringlist.Free;
  inherited;
end;

procedure TMMSLibraryForm.DataTreeGetText(Sender: TBaseVirtualTree;
  Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType;
  var CellText: WideString);
var
  i:Integer;
begin

//这是关键的步骤:将stringlist和datatree绑定。这个函数的解释如下:

(**********************************************************************************************************

property OnGetText: TVSTGetTextEvent;
Description

This is one of the fundamental string tree events which must always be handled. The string tree will fire this event every time when it needs to know about the text of a specific node and column. This is mainly the case when the node appears in the visible area of the tree view (in other words it is not scrolled out of view) but also on some other occasions, including streaming, drag and drop and calculating the width of the node.  

The node text is distinguished between two text types:  

  • Normal text: If TextType is ttNormal return the main node caption for the specified column.
  • Static text: All text that you return when TextType is ttStatic will be displayed right beside the normal text (or left to it if the columns BidiMode is not bdLeftToRight, i.e. the column has right-to-left layout). Static text is used only for informational purposes; it cannot be selected or dragged and if the column is not wide enough to show all text it will not be shortened with an ellipsis (...) as normal text. The string tree will only query for static text if the StringOptions (see TreeOptions) include toShowStaticText. This is off by default.  

When this event is fired the text parameter will always be initialized with the value of property DefaultText. To handle the event get your node data and then extract the string for the appropriate column and TextType.

Notes
Be sure that your event handler only contains absolutely necessary code. This event will be fired very often - easily a 

few hundred times for medium sized trees with some columns defined when the tree is repainted completely. 

For example it is far too slow to use Locate() on some Dataset, a database query result or table, and then get the text 

from some TField. This may only work with in-memory tables or a client dataset. When you initialize your node data do 

some caching and use these cached values to display the data.

************************************************************************************************************)

    for i:=0 to stringlist.Count div 5 -1 do
    begin
      case Column of
        0:CellText:=stringlist[node.index * 5];
        1:CellText:=stringlist[node.index * 5+1];
        2:CellText:=stringlist[node.index * 5+2];
        3:CellText:=stringlist[node.index * 5+3];
        4:CellText:=stringlist[node.index * 5+4];
      end;
    end;

end;

//这个函数居然可以不用也可以连数据库,奇怪?

procedure TMMSLibraryForm.DataTreeInitNode(Sender: TBaseVirtualTree;
  ParentNode, Node: PVirtualNode;
  var InitialStates: TVirtualNodeInitStates);
var
  Data: ^Int64;
begin
//  Data := Sender.GetNodeData(Node);
//  Data^ := Node.Index;
end;

end.



文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

文章页数:[1] 


放大字体显示 缩小字体显示 打印文章 推荐给朋友
热门文章
·使用C#编写Windows Forms应用程序(转)-.NET教程,Windows开发
·C#学习杂记-.NET教程,C#语言
·将文本格式的文章转换为html/xml格式文本的功能封装到Javabean-JSP教程,Java技巧及代码
·用java小程序applet实现无限级树结构-JSP教程,Java技巧及代码
·《.net编程先锋C#》第一章 C#简介-.NET教程,C#语言
·企业人事信息管理系统1.0-ASP教程,数据库相关
·《.net编程先锋C#》第三章 第一个C#应用程序-.NET教程,C#语言
·《.net编程先锋C#》第二章 理论基础-公用语言 运行环境-.NET教程,C#语言
·Windows应用程序调试必备的--符号文件(Symbols)-.NET教程,评论及其它
·c#反编译微软msdn2003的帮助文档,并将反编译结果保存到一个sqlserver数据库中
最新文章
·vista的新特性:懒人的安全感_windows vista
·photoshop为情侣照片打造韩式梦幻相框_photoshop教程
·专访seobbs站长乐思蜀_站长访谈
·嘟嘟网络陈艺光:诚信是一种信仰_站长访谈
·俺自己黄修源:只问耕耘,不问收获_站长访谈
·李向华:如何提高论坛用户粘性_站长访谈
·给windows vista系统网络和共享中心“换脸”_windows vista
·胡宪东谈搜索优化的实战_站长心得
·网站快速成功的九个步骤_站长心得
·google pr数字背后的含意_google推广
相关主题
  • datatable操作中的性能问题_asp.net技巧
  • DataTable.Select使用小心得-.NET教程,数据库应用
  • DataTable控件的使用-.NET教程,组件控件开发
  • DataTable中数据记录的统计-ASP教程,数据库相关
  • 西部数码虚拟主机

    友情链接
    CNNIC 西部数码
    万网 自助建站
    虚拟主机 asp空间
    域名注册 域名
    域名申请 主页空间
    论坛空间 网站空间
    国际域名 虚拟空间
    空间租用 DDOS防火墙
    成都主机托管 四川主机托管
    主机租用 服务器租用
    网站目录 自助建站
    虚拟主机 网址大全
    软件下载
    自助链接
    虚拟主机资讯 特价虚拟主机
    版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
    关于我们:站长天空:专业提供最新的站长资讯、在线教程、虚拟主机权威评测、虚拟主机性能对比、网站制作教程,开发教程,站长工具。包括网页制作教程、冲浪宝典、编程参考、操作系统、软件教学、行业动态等。
    特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
    发表评论 打印  刷新     关闭