翻譯|其它|編輯:郝浩|2006-06-30 13:49:00.000|閱讀 2476 次
概述:
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
窗體的界面設(shè)置以及控件的添加、屬性設(shè)置均按照《在Delphi中用ADO控件打開Access文件》一文,只是在DBRrid1的DrawDataCell事件過程中添加如下代碼:
procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect; Field:
TField; State: TGridDrawState);
var Temp:string; //說明一個字符串變量,用于暫存放單精度的數(shù)值
begin
with(Sender as TDBGrid).Canvas do
begin
FillRect(Rect);
//對字段的值的數(shù)據(jù)類型進(jìn)行判斷,如果是浮點(diǎn)型則進(jìn)行格式設(shè)置
if (Field.DataType = ftFloat) then
begin
if not (field.IsNull)
then
//當(dāng)字段的值不為空時,調(diào)用format函數(shù),對數(shù)值的格式進(jìn)行設(shè)置
{格式字符串‘%8.4f'表示顯示長度為8,以4位小數(shù)的形式輸出。如果要改變輸出的長度及小數(shù)位數(shù),只須修改格式字符串即可。如要保留兩位小數(shù),則用‘%8.2'。更多的信息,請參考Delphi幫助文檔}
temp:=
format(‘%8.4f',[strtofloat(field.asstring)]);
TextOut(Rect.Right-TextWidth(temp)-3,Rect.Top+3, temp);//左對齊畫出單元
end
else
TextOut(Rect.Left+2,Rect.Top+3,Field.AsString);
end;
end;
另外,我們在錄入數(shù)據(jù)時習(xí)慣于按下回車(Enter)鍵就將輸入光標(biāo)移到下一單元格,要實(shí)現(xiàn)這一功能,只需在DBGrid1的OnKeyPress事件過程中添加如下代碼:
procedure TForm1.DBGrid1KeyPress(Sender: TObject; var Key: Char);
begin
if Key=#13 then //如果按鍵為ENTER鍵
if DBGrid1.selectedindex <(DBGrid1.fieldcount -1)then
//如果當(dāng)前活動焦點(diǎn)小于當(dāng)前數(shù)據(jù)網(wǎng)格的字段數(shù)
DBGrid1.selectedindex:=
DBGrid1.selectedindex +1 //轉(zhuǎn)入該數(shù)據(jù)的下一個字段
else
begin
if not ADOtable1.Eof then
//如果不是ADOTABLE1的最后一條記錄
begin
//指向下一條記錄
ADOtable1.Next;
DBGrid1.SelectedIndex:=0; //將當(dāng)前的活動焦點(diǎn)定位在該記錄的第一個字段中
end;
end;
end;
注:以上程序在Win98 SE中文版及Delphi5上調(diào)試通過。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn