新手上路:第一课:操作DBGrid;
DBGrid是我们数据库编辑中不可缺少的一个控件,如果能对它进行熟练的操作,会让程序更人性化;
以下就以一个对DBGrid的操作为例来说明如何改变DBGrid的背景、和对其进行编辑
---------------------------------------------------------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DBCtrls, DB, DBTables, Grids, DBGrids;
type
TForm1 = class(TForm)
DataSource1: TDataSource;
DBGrid1: TDBGrid;
Table1: TTable;
DBComboBox1: TDBComboBox;
procedure DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
Field: TField; State: TGridDrawState);
procedure DBGrid1ColExit(Sender: TObject);
procedure DBGrid1KeyPress(Sender: TObject; var Key: Char);
procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
Field: TField; State: TGridDrawState);
begin
if (gdFocused in State) then
begin
if ( Field.FieldName = DBComboBox1.DataField ) then
begin
DBComboBox1.Left := Rect.Left + DBGrid1.Left;
DBComboBox1. := Rect. + DBGrid1.top;
DBComboBox1.Width := Rect.Right - Rect.Left;
DBComboBox1.Height := Rect.Bottom - Rect.;
DBComboBox1.Visible := True;
end;
end;
end;
procedure TForm1.DBGrid1ColExit(Sender: TObject);
begin
If DBGrid1.SelectedField.FieldName = DBComboBox1.DataField then
begin
DBComboBox1.Visible := false;
end;
end;
procedure TForm1.DBGrid1KeyPress(Sender: TObject; var Key: Char);
begin
if (key <> chr(9)) then
begin
if (DBGrid1.SelectedField.FieldName=DBComboBox1.DataField) then
begin
DBComboBox1.SetFocus;
SendMessage(DBComboBox1.Handle, WM_Char, word(Key), 0);
end;
end;
end;
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if Table1.FieldByName(SIZE).AsInteger = 2 then//仅仅是一个例子罢了
begin
DBGrid1.Canvas.Font.Color:=clred;
DBGrid1.Canvas.Brush.color:=clyellow;
end else
begin
DBGrid1.Canvas.Font.Color:=clblue;
DBGrid1.Canvas.Brush.color:=clBtnFace;
end;
DBGrid1.DefaultDrawColumnCell(rect,datacol,column,state);//这一步不可少的,嘻嘻嘻嘻,俺不喜欢偷懒的;
end;
end.
------------------------------------------------------------------
当然,您可以用TComboBox或者是TListBox都可以,在这我,我用的是TDBComboBox,用它有一个好处就是它可以自动的检索数据集里的数据(和放在DBGrid外边不一样的);
good
very good
好人
这些都是操作dbgrid的经典用法啊
不錯,這項技術,我也是前幾天學到的。
难得你这么有心,只不过注释少了点,呵呵!
不錯
想你學習
如果再加一些注释就更好了
好心人!!
订张座位
洗眼以对
up
ihihonline (小小->不要再流浪!!)
你为什么有200啊?
接分!
谢谢
CSDN多点这些帖子就是我辈之福了
小小,辛苦你了
不过最好能把所有的这些基本内容都放在一起,放在TOP
象ASM论坛一样
支持
我来要分的,好久没发言了
up
人家贴出来你爱看就看不爱看拉倒,批夸什么啊~
学习dbgrid,还有楼主的精神!
希望早日也多几颗星!本不想劳各位兄长的,但希望多几颗星,所以还是原谅我吧!
http://www.csdn.net/expert/topic/965/965509.xml?temp=.7913019
我试过了,不过有问题,也不是从你这学的,前三个过程中,当你改变了DBGRID的字段时和QUERY中的字段改变时,问题就出现了,也请大家测试一下
支持,收藏,呵呵