Berikut ini adalah contoh program sederhana, bagaimana cara meload database dan ditampilkan ke listview..
oke langsung saja.. :D
buatlah desain form kira2 seperti gambar berikut :
oke langsung saja.. :D
buatlah desain form kira2 seperti gambar berikut :
Berikut source code nya lengkap :
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ComCtrls, Grids, DBGrids, XPMan, StdCtrls, ExtCtrls;typeTCustomSortStyle = (cssAlphaNum, cssNumeric, cssDateTime); TForm1 = class(TForm) DBGrid1: TDBGrid; ListView1: TListView; Button1: TButton; XPManifest1: TXPManifest; Button2: TButton; StatusBar1: TStatusBar; Timer1: TTimer; procedure Button2Click(Sender: TObject); procedure Button1Click(Sender: TObject); procedure ListView1ColumnClick(Sender: TObject; Column: TListColumn); procedure Timer1Timer(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1; { variable to hold the sort style } LvSortStyle: TCustomSortStyle; { array to hold the sort order } LvSortOrder: array[0..4] of Boolean;implementationuses Unit2;{$R *.dfm}function CustomSortProc(Item1, Item2: TListItem; SortColumn: Integer): Integer; stdcall;var s1, s2: string; i1, i2: Integer; r1, r2: Boolean; d1, d2: TDateTime; { Helper functions } function IsValidNumber(AString : string; var AInteger : Integer): Boolean; var Code: Integer; begin Val(AString, AInteger, Code); Result := (Code = 0); end; function IsValidDate(AString : string; var ADateTime : TDateTime): Boolean; begin Result := True; try ADateTime := StrToDateTime(AString); except ADateTime := 0; Result := False; end; end; function CompareDates(dt1, dt2: TDateTime): Integer; begin if (dt1 > dt2) then Result := 1 else if (dt1 = dt2) then Result := 0 else Result := -1; end; function CompareNumeric(AInt1, AInt2: Integer): Integer; begin if AInt1 > AInt2 then Result := 1 else if AInt1 = AInt2 then Result := 0 else Result := -1; end;begin Result := 0; if (Item1 = nil) or (Item2 = nil) then Exit; case SortColumn of -1 : { Compare Captions } begin s1 := Item1.Caption; s2 := Item2.Caption; end; else { Compare Subitems } begin s1 := ''; s2 := ''; { Check Range } if (SortColumn < Item1.SubItems.Count) then s1 := Item1.SubItems[SortColumn]; if (SortColumn < Item2.SubItems.Count) then s2 := Item2.SubItems[SortColumn] end; end; { Sort styles } case LvSortStyle of cssAlphaNum : Result := lstrcmp(PChar(s1), PChar(s2)); cssNumeric : begin r1 := IsValidNumber(s1, i1); r2 := IsValidNumber(s2, i2); Result := ord(r1 or r2); if Result <> 0 then Result := CompareNumeric(i1, i2); end; cssDateTime : begin r1 := IsValidDate(s1, d1); r2 := IsValidDate(s2, d2); Result := ord(r1 or r2); if Result <> 0 then Result := CompareDates(d1, d2); end; end; { Sort direction } if LvSortOrder[SortColumn + 1] then Result := - Result;end;procedure TForm1.Button1Click(Sender: TObject);vari : Integer;LC : TListColumn;LI : TListItem;beginButton2Click(Sender);// load kolomfor i:= 0 to DM.tb.FieldCount-1 dobeginLC := self.ListView1.Columns.Add;LC.Caption := UpperCase(DM.tb.Fields [i]. FieldName);LC.Width := 100;end;// load recordDM.tb.First;while not DM.tb.Eof dobeginLI := self.ListView1.Items.Add;LI.Caption := DM.tb.Fields [0]. AsString;for i:= 1 to DM.tb.FieldCount-1 dobeginLI.SubItems.Add (DM.tb.Fields [i]. AsString);end;DM.tb.Next;end;end;procedure TForm1.Button2Click(Sender: TObject);var i:integer;beginfor i := ListView1.Items.Count - 1 downto 0 doListView1.Items.Delete(i);for i := ListView1.Columns.Count - 1 downto 0 doListView1.Columns[i].Destroy;end;procedure TForm1.ListView1ColumnClick(Sender: TObject; Column: TListColumn);begin case Column.Index of 0 : LvSortStyle := cssNumeric; 1 : LvSortStyle := cssAlphaNum; 2 : LvSortStyle := cssAlphaNum; end; { Call the CustomSort method } ListView1.CustomSort(@CustomSortProc, Column.Index -1); listview1.Column[1]; { Set the sort order for the column} LvSortOrder[Column.Index] := not LvSortOrder[Column.Index];end;procedure TForm1.Timer1Timer(Sender: TObject);var s : string;begins:='Untuk mensortir klik pada kolom';if statusbar1.Panels[0].Text = s then statusbar1.Panels[0].Text:=''else statusbar1.Panels[0].Text:=s;end;end.
Hasil screen shot program :
Program dan source code bisa di download DISINI
waduh, asal copas nih. coding php html tp judulnya Delphi
ReplyDelete