控件设置
- ViewStyle 设为vsReport
- ReadOnly 设为True
- GridLines 设为True
- RowSelect 设为True
增加数据
var
i : integer;
begin
//增加
i := ListView1.Items.Count;
with ListView1.Items.Add do
begin
Caption:= IntToStr(i);
SubItems.Add('第 '+IntToStr(i)+' 行');
SubItems.Add('第三列内容');
end;
end;
删除选中的行
var
i : integer;
begin
for i:=ListView1.Items.Count-1 downto 0 do //直接用to会报错
if ListView1.Items[i].Selected then //i=ListView1.Selected.index
begin
ListView1.Items.Delete(i); //删除当前选中行
end;
end;
按标题删除
var
i : integer;
begin
//按标题删除
for i:=ListView1.Items.Count-1 downto 0 Do
if ListView1.Items[i].Caption = Edit1.Text then
begin
ListView1.Items.Item[i].Delete(); //删除当前选中行
end;
end;
读取某行某列内容
begin
Edit1.Text := listview1.Items[0].Caption; //读第i行第1列
Edit2.Text := listview1.Items[0].SubItems.strings[0]; //读第i行第2列
Edit3.Text := listview1.Items[0].SubItems.strings[1]; //读第i行第3列
end;
修改某行某列内容
begin
//必须原先存在数据,才能去修改,可以用Add('')新建数据初始化
listview1.Items[0].Caption:= Edit1.Text;
listview1.Items[0].SubItems.strings[0]:= Edit2.Text;
listview1.Items[0].SubItems.strings[1]:= Edit3.Text;
end;
清空内容
begin
ListView1.Clear;
end;
设置焦点
//类似手动选中某一行
begin
ListView1.SetFocus;
ListView1.ItemIndex:=3;
end;
上下移动条目
procedure ListViewItemMoveUpDown(lv: TListView; Item: TListItem; MoveUp, SetFocus: Boolean);
var
DestItem: TListItem;
begin
if (Item = nil) or ((Item.Index - 1 < 0) and MoveUp) or ((Item.Index + 1 >= lv.Items.Count) and (not MoveUp)) then
Exit;
lv.Items.BeginUpdate;
try
if MoveUp then
DestItem := lv.Items.Insert(Item.Index - 1)
else
DestItem := lv.Items.Insert(Item.Index + 2);
DestItem.Assign(Item);
lv.Selected := DestItem;
Item.Free;
finally
lv.Items.EndUpdate;
end;
if SetFocus then
lv.SetFocus;
DestItem.MakeVisible(False);
end;
procedure TForm3.Button8Click(Sender: TObject);
begin
ListViewItemMoveUpDown(ListView1, ListView1.Selected, True, True);//上移
end;
procedure TForm3.Button9Click(Sender: TObject);
begin
ListViewItemMoveUpDown(ListView1, ListView1.Selected, False, True);//下移
end;
读出选中行信息
var
i: integer;
begin
if ListView1.Selected <> nil then
begin
memo1.Lines.Add('Caption:' + ListView1.Selected.Caption);
for I := 0 to ListView1.Selected.SubItems.Count-1 do
begin
memo1.Lines.Add('SubItems:' + ListView1.Selected.SubItems[i]);
end;
end;
end;
点击正反排序
m_bSort: boolean = false; // 控制正反排序的变量
function CustomSortProc(Item1, Item2: TListItem; ParamSort: integer): integer; stdcall;
var
txt1, txt2: string;
Num1, Num2: Integer;
begin
if ParamSort <> 0 then //点击subItems
begin
try
txt1 := Item1.SubItems.strings[ParamSort - 1];
txt2 := Item2.SubItems.strings[ParamSort - 1];
if m_bSort then
begin
Result := CompareText(txt1, txt2);
end else begin
Result := -CompareText(txt1, txt2);
end;
except
end;
end
else //点击caption
begin
txt1:= Item1.Caption;
txt2:= Item2.Caption;
// 将字符串转换为数字
Num1 := StrToIntDef(txt1, MaxInt);
Num2 := StrToIntDef(txt2, MaxInt);
// 进行数字比较
Result := Num1 - Num2;
//如果都不是数字安装默认排序法排序
if (Num1 = MaxInt) and (Num2 = MaxInt) then
Result := AnsiCompareText(txt1, txt2);
if m_bSort then
begin
Result:= -Result;
end
end;
end;
procedure TForm3.ListView1ColumnClick(Sender: TObject; Column: TListColumn);
begin
ListView1.CustomSort(@CustomSortProc, Column.Index);
m_bSort := not m_bSort;
end;
每行item显示不一样的颜色
添加AdvancedCustomDrawItem事件
procedure TForm3.ListView1AdvancedCustomDrawItem(Sender: TCustomListView; Item: TListItem;
State: TCustomDrawState; Stage: TCustomDrawStage; var DefaultDraw: Boolean);
begin
if Odd(Item.Caption.ToInteger) then
begin
Sender.Canvas.Font.Color := clWindowText;
Sender.Canvas.Font.Name := '宋体';
Sender.Canvas.Font.Size := 12;
Sender.Canvas.Brush.Color := $d0d0f2;
end
else
begin
Sender.Canvas.Font.Color := clWindowText;
Sender.Canvas.Font.Name := '宋体';
Sender.Canvas.Font.Size := 12;
Sender.Canvas.Brush.Color := $e6c8c8;
end
end;
最后上个Demo合照,点击下载Demo源码