2011-12-23 177 views
1

我的代碼是下面,它的正常工作,但是,但在編譯程序後,我看到所有的全名和全國上市垂直類似:如何在delphi中正確使用ListView?

_________________________________
Fullname1
國家1
Fullname2
COUNTRY2
Fullname3
國家3
等...

SQLQuery1.SQL.Text := 'SELECT * FROM users where user_age="'+age+'"'; 
SQLQuery1.Open; 
rec := SQLQuery1.RecordCount; 

SQLQuery1.First; // move to the first record 
ListView1.Visible := false; 
if rec>0 then 
begin 
while(not SQLQuery1.EOF)do begin 
ListView1.Visible := true; 
     // do something with the current item 
ListView1.AddItem('Full name: '+SQLQuery1['fullname'], Self); 
ListView1.AddItem('Country: '+SQLQuery1['cntry'], Self); 

    // move to the next record 

SQLQuery1.Next; 

end; 

但我想是這樣的:

enter image description here

+0

您正在添加項目而不是子項目!您需要添加該項目,然後將該返回值添加到SubItems集合中。 – Lloyd 2011-12-23 11:42:35

+0

如何添加分項目? – 2011-12-23 11:43:54

+0

除了使用子項,你可能想要設置ListView的可見性屬性OUTside循環... – 2011-12-23 11:50:04

回答

18

第一:添加列標題:

var 
    Col: TListColumn; 
begin 
    Col := ListView1.Columns.Add; 
    Col.Caption := 'Name'; 
    Col.Alignment := taLeftJustify; 
    Col.Width := 140; 

    Col := ListView1.Columns.Add; 
    Col.Caption := 'Country'; 
    Col.Alignment := taLeftJustify; 
    Col.Width := 140; 
end; 

然後添加記錄如下:

var 
    Itm: TListItem; 
begin 
    // start of your query loop 
    Itm := ListView1.Items.Add; 
    Itm.Caption := SQLQuery1['fullname']; 
    Itm.SubItems.Add(SQLQuery1['cntry']); 
    // end of your query loop 
end; 

更新:

當然,爲了得到該列表中的截圖,你需要ListView的ViewStyle屬性設置爲vsReport

+0

我懷疑'ViewStyle'也需要設置爲'vsReport'。 – 2011-12-23 12:09:26

+0

你的代碼不起作用,它不包含錯誤,但是當我點擊應該打開包含listview的窗口的按鈕時,什麼也沒有發生!:! – 2011-12-23 12:19:24

+0

@DavidHeffernan:是的,我認爲OP已經想通了。也許不會。 – 2011-12-23 12:20:48

3

你的代碼應該看起來像:

var 
    ListItem: TListItem; 

    ... 

    ListView.Items.BeginUpdate; 
    try 
    while(not SQLQuery1.EOF)do begin 
     ListItem:= ListView.Items.Add; 
     ListItem.Caption:= 'Full name: '+SQLQuery1['fullname']; 
     with ListItem.SubItems do begin 
     Add('Country: '+SQLQuery1['cntry']); 
// if you need more columns, add here 
     end; 
     SQLQuery1.Next; 
    end; 
    finally 
    ListView.Items.EndUpdate; 
    end; 

您還應該設置ListView.StylevsReport將listview顯示爲網格。

2

我不知道如何獲得listview到多行,但我知道你沒有正確使用查詢。 因爲它代表了你的代碼有一個SQL注入漏洞,並且在循環內隱式引用'fieldbyname'會使它變慢。

var 
    FullName: TField; 
    Country: TField; 
    ListItem: TListItem; 
begin 
    //Use Params or suffer SQL-injections 
    SQLQuery1.SQL.Text := 'SELECT * FROM users where user_age= :age'; 
    SQLQuery1.ParamByName('age').AsInteger:= age; 
    SQLQuery1.Open; 
    if SQLQuery1.RecordCount = 0 then Exit; 
    //Never use `FieldByName` inside a loop, it's slow. 
    FullName:= SQLQuery1.FieldByName('fullname'); 
    Country:= SQLQuery1.FieldByName('cntry'); 
    ListView1.Style:= vsReport; 

    SQLQuery1.First; // move to the first record 
    SQLQuery1.DisableControls; //Disable UI updating until where done. 
    try 
    ListView1.Items.BeginUpdate; 
    //ListView1.Visible := false; 
    while (not SQLQuery1.EOF) do begin 
     //Code borrowed from @Serg 
     ListItem:= ListView.Items.Add; 
     ListItem.Caption:= 'Full name: '+Fullname.AsString; 
     ListItem.SubItems.Add('Country: '+Country.AsString); 
     SQLQuery1.Next; 
    end; {while} 
    finally 
    SQLQuery1.EnableControls; 
    ListView1.Items.EndUpdate; 
    end; 
end; 
3

德爾福的文檔包含這個example,這正是你想要的。

procedure TForm1.FormCreate(Sender: TObject); 
const 
    Names: array[0..5, 0..1] of string = (
    ('Rubble', 'Barney'), 
    ('Michael', 'Johnson'), 
    ('Bunny', 'Bugs'), 
    ('Silver', 'HiHo'), 
    ('Simpson', 'Bart'), 
    ('Squirrel', 'Rocky') 
    ); 

var 
    I: Integer; 
    NewColumn: TListColumn; 
    ListItem: TListItem; 
    ListView: TListView; 
begin 
    ListView := TListView.Create(Self); 
    with ListView do 
    begin 
    Parent := Self; 
    Align := alClient; 
    ViewStyle := vsReport; 

    NewColumn := Columns.Add; 
    NewColumn.Caption := 'Last'; 
    NewColumn := Columns.Add; 
    NewColumn.Caption := 'First'; 

    for I := Low(Names) to High(Names) do 
    begin 
     ListItem := Items.Add; 
     ListItem.Caption := Names[I][0]; 
     ListItem.SubItems.Add(Names[I][2]); 
    end; 
    end; 
end; 

對於所有那些德爾福的文檔被大肆誹謗,它通常有這樣的非常有用的例子。示例的網關頁面爲here,示例甚至可在sourceforge上使用,因此您可以使用您最喜歡的svn客戶端檢查它們。

+3

我不知道這些例子是在sourceforge上。對他們很好。 – 2011-12-23 13:00:26

+0

@Marjan在我搜索這個答案之前,我都沒有做過。 – 2011-12-23 13:51:44

0
Procedure TForm1.GetUsers; 
var 
    ListItem: TListItem; 
begin 
    try 
    ListView1.Items.BeginUpdate; 
    try 
     ListView1.Clear; 
     MySQLQuery.SQL.Clear; 
     MySQLQuery.SQL.Add('select * from users;'); 
     MySQLQuery.Open; 
     while (not MySQLQuery.EOF) do 
     begin 
     ListItem := ListView1.Items.Add; 
     ListItem.Caption:= VarToSTr(MySQLQuery['username']); 
     with ListItem.SubItems do 
      begin 
      Add(VarToSTr(MySQLQuery['password'])); 
      Add(VarToSTr(MySQLQuery['maxscore'])); 
      end; 
     MySQLQuery.Next; 
     end; 
     MySQLQuery.Close; 
    finally 
     ListView1.Items.EndUpdate; 
    end; 
    except 
    on E: Exception do 
     MessageDlg(PWideChar(E.Message), TMsgDlgType.mtError, [TMsgDlgBtn.mbOK], 0); 
    end; 
end; 
相關問題