请教关于LISTVIEW添加修改纪录的一个问题!请不吝赐教!(100分)

  • 主题发起人 主题发起人 江南大米
  • 开始时间 开始时间

江南大米

Unregistered / Unconfirmed
GUEST, unregistred user!
LISTVIEW做为连接数据库前,数据暂存区域!输完一条纪录保存一次就在LISTVIEW中
增加一条,
var
listitem:Tlistitem;
begin
listitem:=listview1.Items.Add;
listitem.Caption:=Edit1.Text;
listitem.SubItems.Add(Edit2.Text);
end;
最后全部提交到数据库!
期待解决问题是:如果在过程中输错一条纪录在LISTVIEW中还没有提交!我用
ListView1DblClick
Edit1.Text:=listview1.Selected.Caption;
Edit2.Text:=listview1.Selected.SubItems.Strings[0];
从新取回纪录,但是如何在再次保存的时候是修改原纪录而不是又增加一条纪录!
 
帮忙UP的也有分!别让它掉下来!
 
Please refer:

procedure TForm1.CopyDataClick(Sender: TObject);

var
SavePlace: TBookmark;
PrevValue: Variant;
begin
with Table1 do
begin
{ get a bookmark so that we can return to the same record }
SavePlace := GetBookmark;

{ move to prior record}

FindPrior;

{ get the value }

PrevValue := Fields[0].Value;

{Move back to the bookmark

this may not be the next record anymore
if something else is changing the dataset asynchronously }
GotoBookmark(SavePlace);
{ Set the value }
Fields[0].Value := PrevValue;
{ Free the bookmark }
FreeBookmark(SavePlace);
end;

end;
 
取出的时候,保存LISTVIEW中的索引,然后,修改完写回这个索引的具体值就可以了。
 
使用一个变量记录一下呀
 
肯定要保留一个全局的记录号,便于修改:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls, ImgList, ExtCtrls, jpeg;
type
TForm1 = class(TForm)
ListView1: TListView;
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
Button2: TButton;
ComboBox1: TComboBox;
ImageList1: TImageList;
ImageList2: TImageList;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Image1: TImage;
Label4: TLabel;
procedure ListView1DblClick(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure ComboBox1Change(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Image1DblClick(Sender: TObject);
private
selItem: integer;
procedure additme;
end;
var
Form1: TForm1;
implementation
uses Unit2;
{$R *.DFM}

procedure TForm1.additme;
var
listitem: Tlistitem;
begin
if (Edit1.text = '') and (edit2.text = '') then exit;
listitem := listview1.Items.Add;
listitem.Caption := Edit1.Text;
listitem.SubItems.Add(Edit2.Text);
end;

procedure TForm1.ListView1DblClick(Sender: TObject);
var
Old_Style:TViewStyle;
begin
if listview1.Items.Count<1 then exit;
if not Assigned(listview1.Selected)then
begin
Button1.SetFocus;
exit;
end;
Old_Style:=ListView1.ViewStyle;
if Old_Style = vsReport then ListView1.ViewStyle := vsIcon;
Edit1.Text := listview1.Selected.Caption;
Edit2.Text := listview1.Selected.SubItems.Strings[0];
selItem := listview1.Selected.Index;
ListView1.ViewStyle := Old_Style;
if edit1.Font.Color <> clRed then edit1.Font.Color:=clRed;
edit2.SetFocus;
end;

procedure TForm1.Button1Click(Sender: TObject);// Caption:='添加'
begin
additme;
end;

procedure TForm1.ComboBox1Change(Sender: TObject);
begin
case ComboBox1.ItemIndex of
0:
ListView1.ViewStyle := vsIcon;
1:
ListView1.ViewStyle := vsSmallIcon;
2:
ListView1.ViewStyle := vsList;
3:
ListView1.ViewStyle := vsReport;
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
ListView1.ViewStyle := vsReport;
ComboBox1.text := '详细报告模式';
with ComboBox1.Items do
begin
Add('大图标模式');
Add('小图标模式');
Add('列表模式');
Add('详细报告模式');
end;
end;

procedure TForm1.Button2Click(Sender: TObject);// Caption:='修改'
begin
listview1.Items[selItem].Caption := Edit1.Text;
listview1.Items[selItem].SubItems.Text := Edit2.Text;
if edit1.Font.Color = clRed then edit1.Font.Color:=clBlack;
end;

procedure TForm1.Image1DblClick(Sender: TObject);
begin
Form2.ShowModal;// 显示其他 Form
end;

end.
 
解决!谢谢各位!论功行赏!
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
928
SUNSTONE的Delphi笔记
S
后退
顶部