ListView修改列

本文转载:http://hi.baidu.com/letwin/blog/item/1356262236f90faf4623e89b.html

 

实现思路:双击时找到当前选择的行,并通过列遍历找到具体的列,找到行列就可以定位单元格了,然后读出来数据到Edit,同时设置Edit显示,当离开Edit时把具体的值写回ListView。

 

窗体的试验控件如下:

  object LV: TListView
    Left = 8
    Top = 8
    Width = 297
    Height = 289
    Columns = <
      item
        Caption = '姓名'
        Width = 70
      end
      item
        Caption = '工作'
        Width = 80
      end
      item
        Caption = '年龄'
        Width = 55
      end
      item
        Caption = '刘添加'
        Width = 80
      end>
    GridLines = True
    ReadOnly = True
    RowSelect = True
    TabOrder = 0
    ViewStyle = vsReport
    OnDblClick = LVDblClick
  end
  object Button1: TButton
    Left = 8
    Top = 344
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 1
    OnClick = Button1Click
  end
  object edtAge: TEdit
    Left = 8
    Top = 312
    Width = 121
    Height = 21
    BevelEdges = []
    BevelInner = bvNone
    BevelOuter = bvNone
    TabOrder = 2
    Visible = False
    OnChange = edtAgeChange
    OnExit = edtAgeExit
    OnKeyPress = edtAgeKeyPress
  end


 

Button1代码如下:

 

var
  tmpItem: TListItem;
begin
  tmpItem := lv.Items.Add;
  tmpItem.Caption := 'a';
  tmpItem.SubItems.Add('工任地');
  tmpItem.SubItems.Add('0');
  tmpItem.SubItems.Add('YY');
end;


 

新添加的TEdit事件如下:

procedure TForm1.edtAgeChange(Sender: TObject);
begin
  LV.Selected.SubItems[nCurIndex - 1] := edtAge.Text;
end;

procedure TForm1.edtAgeExit(Sender: TObject);
begin
  if edtAge.Text <> '' then
  begin
    LV.Selected.SubItems[nCurIndex-1] := edtAge.Text;
    edtAge.Visible := False;
  end;
end;

procedure TForm1.edtAgeKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
    edtAge.OnExit(Sender);//失去焦点
end;

 


主要在下面的双击或SelectItem事件中(本例选择双击事件):

var
  W, X, nCount: Integer;
  Rect: TRect;
  Pos: TPoint;
  nCol: Integer;
begin
  if Assigned(LV.Selected) then//判断双击的区域是否为有效区域
  begin
    Pos := LV.ScreenToClient(Mouse.CursorPos);
    nCount := LV.Columns.Count;
    X := -GetScrollPos(LV.Handle, SB_HORZ);

    for nCol := 0 to nCount - 1 do
    begin
      W := LV.Columns[nCOL].Width;//nCOL是你要修改的那列宽度
      if Pos.X <= X + W then
      begin
        Break;
      end;
      X := X + W
    end;

    nCurIndex := nCol;
    if nCol = nCount then
    begin
      Exit;
    end;

    if nCol = 0 then
    begin
      LV.DeleteSelected; //若添加了此行,则表示点击了第一列会删除此行
      Exit; //第1列不允许编辑
    end;

    if LV.Columns[nCol].Caption <> '年龄' then  //不是要编辑的那列
    begin
      LV.DeleteSelected;
      Exit;
    end;

    if X < 0 then
    begin
      W := W + X;
      X := 0;
    end;

    Rect := LV.Selected.DisplayRect(drBounds);

    edtAge.SetBounds(X, Rect.Top, W, Rect.Bottom- Rect.Top + 3);

    edtAge.Parent := LV;
    edtAge.Top := LV.Selected.Top;
    edtAge.Text := LV.Selected.SubItems[nCurIndex-1];
    edtAge.Visible := True;
    edtAge.SetFocus;
  end;
end;


 

最后,别忘了给全局变量做个声明与初始化:

var
  Form1: TForm1;
  nCurIndex: integer;//声明

procedure TForm1.FormCreate(Sender: TObject);
begin
  nCurIndex := 0;
end;


 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Delphi中使用TListView来显示Access数据库的数据,并将数据显示在指定的上,您可以按照以下步骤进行操作: 1. 添加TListView控件到您的窗体上。 2. 设置TListView的ViewStyle属性为vsReport,这将使其以报表视图显示。 3. 在设计时添加所需的。您可以在Object Inspector中添加,也可以在运行时使用以下代码添加: ``` ListView1.Columns.Add.Caption := '标题1'; ListView1.Columns.Add.Caption := '标题2'; ``` 4. 连接到Access数据库。您可以使用ADOConnection组件连接到Access数据库。设置ADOConnection的ConnectionString属性来指定数据库的连接字符串,例如: ``` ADOConnection1.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\your_database.mdb'; ``` 5. 使用ADOQuery组件执行SQL查询并获取结果集。设置ADOQuery的SQL属性为查询语句,例如: ``` ADOQuery1.SQL.Text := 'SELECT 1, 2 FROM 表名'; ADOQuery1.Open; ``` 6. 遍历结果集并将数据添加到TListView中的每一行。例如: ``` while not ADOQuery1.Eof do begin ListItem := ListView1.Items.Add; ListItem.Caption := ADOQuery1.FieldByName('1').AsString; ListItem.SubItems.Add(ADOQuery1.FieldByName('2').AsString); ADOQuery1.Next; end; ``` 以上是一个简单的示例,您可以根据您的具体情况进行修改和扩展。请注意,您需要使用合适的ADO组件(例如ADOConnection、ADOQuery)和连接字符串来连接到Access数据库。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值