?? dbgrid.htm
字號(hào):
if (Field.FieldName = DBCheckBox1.DataField) then
begin
if TableGridDataCheckBox.AsBoolean then
DBGrid1.Canvas.Draw(Rect.Left,Rect.Top,ImageTrue.Picture.Bitmap)
else
DBGrid1.Canvas.Draw(Rect.Left,Rect.Top,ImageFalse.Picture.Bitmap)
end
end;</PRE>
<P><HR></P>
<P>It's the very last part we're most interested in.
If the state is not gdFocused and the column in CheckBox then this last bit executes.
All it does is check the value of the data in the field and if it's true it shows the TRUE.BMP
otherwise it shows the FALSE.BMP.
I created the bit maps so they are indented so you can tell the difference between a focused and unfocused cell. Make onColExit look like this;</P>
<P><HR></P>
<PRE>procedure TForm1.DBGrid1ColExit(Sender: TObject);
begin
If DBGrid1.SelectedField.FieldName = DBLookupCombo1.DataField then
DBLookupCombo1.Visible := false
else If DBGrid1.SelectedField.FieldName = DBCheckBox1.DataField then
DBCheckBox1.Visible := false
else If DBGrid1.SelectedField.FieldName = DBComboBox1.DataField then
DBComboBox1.Visible := false;
end;</PRE>
<P><HR></P>
<P>Edit onKeyPress to;</P>
<P><HR></P>
<PRE>procedure TForm1.DBGrid1KeyPress(Sender: TObject; var Key: Char);
begin
if (key <> chr(9)) then
begin
if (DBGrid1.SelectedField.FieldName = DBLookupCombo1.DataField) then
begin
DBLookupCombo1.SetFocus;
SendMessage(DBLookupCombo1.Handle, WM_Char, word(Key), 0);
end
else if (DBGrid1.SelectedField.FieldName = DBCheckBox1.DataField)
then
begin
DBCheckBox1.SetFocus;
SendMessage(DBCheckBox1.Handle, WM_Char, word(Key), 0);
end
else if (DBGrid1.SelectedField.FieldName = DBComboBox1.DataField)
then
begin
DBComboBox1.SetFocus;
SendMessage(DBComboBox1.Handle, WM_Char, word(Key), 0);
end;
end;
end;</PRE>
<P><HR></P>
<P>Finally, here's the last trick. The caption of the checkbox needs to change as the user checks or unchecks the box.
My first thought was to do this in the TDBCheckBox's onChange event, the only problem is that it doesn't have one.
So I had to go back to the Windows API and send another message. "SendMessage(DBCheckBox1.Handle, BM_GetCheck, 0, 0)"
which returns a 0 if the box is unchecked, otherwise it's checked.</P>
<P><HR></P>
<PRE>procedure TForm1.DBCheckBox1Click(Sender: TObject);
begin
if SendMessage(DBCheckBox1.Handle, BM_GetCheck, 0, 0) = 0 then
DBCheckBox1.Caption := ' ' + 'False'
else
DBCheckBox1.Caption := ' ' + 'True'
end;</PRE>
<P><HR></P>
<P>That's it. Hopefully you learned something.
I've tried this technique with dialog boxes. It works and it's simple. Have fun with it.
You don't really need to completely understand it as long as you know how to edit the code and replace the above component names
with with the name of the component you want to drop into the grid.</P>
<H2>REVISED - 7/11/95</H2>
<P>Fred Dalgleish was nice enough to point out 2 stichy points about the Original grid demo.
First, once a component in the grid has the focus it takes 2 Tab presses to move to the next grid cell.
The other has to do with adding new records.</P>
<H3>Problem # 1 - Two Tab Presses Required.</H3>
<P>A component installed in the grid is actually floating over the top of the grid and not part of the grid it self.
So when that component has the focus it takes two tab presses to move to the next cell.
The first tab moves from the floating component to the Grid cell underneath and the second to move to the next grid cell. If this behavior bugs you heres how to fix it.</P>
<P>First in the form that contains grid add private variable called WasInFloater of type boolean, like so.</P>
<P><HR></P>
<PRE>type
TForm1 = class(TForm)
...
...
private
{ Private declarations }
WasInFloater : Boolean;
...
...
end;</PRE>
<P><HR></P>
<P>Next create an onEnter event for the LookupCombo where WasInFloater is set to true.
Then point the onEnter event for each component that goes into the grid at this same single onEnter event.</P>
<P><HR></P>
<PRE>procedure TForm1.DBLookupCombo1Enter(Sender: TObject);
begin
WasInFloater := True;
end;</PRE>
<P><HR></P>
<P>Finally, and here's the tricky part, define the following onKeyUp event for the grid.</P>
<P><HR></P>
<PRE>procedure TForm1.DBGrid1KeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if (Key in [VK_TAB]) and WasInFloater then
begin
SendMessage(DBGrid1.Handle, WM_KeyDown, Key, 0);
WasInFloater := False;
end;
end;</PRE>
<P><HR></P>
<P>What's happening here is that the grid's onKeyUp is sending it self a KeyDown when the focus just switched from one of the floating controls.
This solution handles both tab and shift-tab.</P>
<H3>Problems #2 - New record disappears when component gets focus</H3>
<P>The second problem is that if you press add record on the navigator in the demo a new record is added but then when you click on one
of the components installed in the grid the new record disappears. The reason for this is that there is a strange grid option called dgCancelOnExit which is
True by default. Set it to False and the above problem goes away.</P>
<P>In my opinion Borland should have had this default set to False to begin with
. I find it getting in the way all the time and based on forum messages I'm not alone.
The option is basically saying that if the grid looses focus then cancel and current edit's! Anyway I've got it
turned off in just about every grid I've ever installed.</P>
<P>Note: This was written by Alec Bergamini, at 75664,1224. His companyis Out & About Productions.</P>
<H1><A NAME="dbgrid2">Sorting Columns in a DBGrid</A></H1>
<I><P>Robert Vivrette - 76416.1373@compuserve.com</P>
</I><P>Many professional applications will display data in grid fields and allow you to sort on any one of the columns simply by clicking on the column header.
Although what is proposed here is not the best way to accomplish this, it is a fairly simple way to mimic the same behavior.</P>
<P>The key hurdle in this problem is the DBGrid itself.
It has no OnClick or OnMouseDown events, so it really was not designed to capture this kind of input.
It does provide an OnDoubleClick, but this really doesn't work too well. What we need is a way to make the column headers clickable.
Enter the THeaderControl component.</P>
<P>THeaderControl is a component that comes in Delphi 2.0 and provides the basic functions that we want.
It can detect clicks on its individual panels, and the panels even go up and down when pressed (like a button).
The key is to connect the THeaderControl to the DBGrid. Here is how it is done:</P>
<P>First, start a new application. Drop a THeaderControl on the form.
It will automatically align to the top edge of the form. Now drop a DBGrid on the form and set its Align property to alClient.
Next, add a TTable, and TDataSource component.
Set the Tables DatabaseName property to DBDEMOS and its TableName to EVENTS.DB.
Set the DataSource's DataSet property to point at Table1 and the DBGrid's DataSource property to point to DataSource1.
Set Table's Active property to False in case it has been turned on. Now the fun begins!</P>
<P>Now we need to setup the THeaderControl component to look like the DBGrid's column headers.
his will be done in code in the Form's FormCreate method. DoubleClick on Form1's OnCreate event and enter the following code:</P>
<P><HR></P>
<PRE>procedure TForm1.FormCreate(Sender: TObject);
var
TheCap : String;
TheWidth,a : Integer;
begin
DBGrid1.Options := DBGrid1.Options - [dgTitles];
HeaderControl1.Sections.Add;
HeaderControl1.Sections.Items[0].Width := 12;
Table1.Exclusive := True;
Table1.Active := True;
For a := 1 to DBGrid1.Columns.Count do
begin
with DBGrid1.Columns.Items[a-1] do
begin
TheCap := Title.Caption;
TheWidth := Width;
end;
with HeaderControl1.Sections do
begin
Add;
Items[a].Text := TheCap;
Items[a].Width := TheWidth+1;
Items[a].MinWidth := TheWidth+1;
Items[a].MaxWidth := TheWidth+1;
end;
try
Table1.AddIndex(TheCap,TheCap,[]);
except
HeaderControl1.Sections.Items[a].AllowClick := False;
end;
end;
Table1.Active := False;
Table1.Exclusive := False;
Table1.Active := True;
end;</PRE>
<P><HR></P>
<P>Since the THeaderControl will be taking the place of the Grid's column headers, we first remove (set to False) the dgTitles option in the DBGrid's Options property.
Then, we add a column to the HeaderControl and set its width to 12. This will be a blank column that is the same width as the Grid's status area on the left.</P>
<P>Next we need to make sure the Table is opened for Exclusive use (no other users can be using it). I will explain why in just a bit.</P>
<P>Now we add the HeaderControl sections. For each one we add, we will be giving it the same text as the caption of that column in the DBGrid.
We loop through the DBGrid columns, and for each one we copy over the column's caption and width. We also set the HeaderControl's MinWidth and
MaxWidth properties to the same as the column width. This will prevent the column from being resized. If you need resizeable columns,
you will need a bit more code, and I wanted to keep this short and sweet.</P>
<P>Now comes the interesting part. We are going to create an index for each column in the DBGrid. The name of the index will be the same as the columns title.
This step is in a try..finally structure because there are some fields that cannot be indexed (Blobs & Memos for example). When it tries to index on these fields,
it will generate an exception. We catch this exception and turn off the ability to click that column. This means that non-indexed columns will not respond to mouse clicks.
The creation of these indexes is why we had to open the table in Exclusive mode. After we are all done, we close the table, set Exclusive off and reopen then table.</P>
<P>One last step. When the HeaderControl is clicked, we need to turn on the correct index for the Table. The HeaderControl's OnSectionClick method should be as follows:</P>
<P><HR></P>
<PRE>procedure TForm1.HeaderControl1SectionClick(
HeaderControl: THeaderControl;
Section: THeaderSection);
begin
Table1.IndexName := Section.Text;
end;</PRE>
<P><HR></P>
<P>That's it! When the column is clicked, the Table's IndexName property is set to the same as the HeaderControl's caption.</P>
<P>Pretty simple, huh? There is a lot of room for improvement however. It would be nice if clicking on a column a second time would reverse the sort order.
Also, column resizing would be a nice added touch. I am going to leave these to you folks!</P>
<H2>Improvements</H2>
<I><P>The Graphical Gnome <rdb@ktibv.nl></P>
</I><P>The improvement over the previous version is in the usage of the fieldname as indexname instead of the caption. </P>
<P>This improves the flexibility. Changes are indicated as italics </P>
<P><HR></P>
<PRE>
procedure TfrmDoc.FormCreate(Sender: TObject);
Var
TheCap : String;
TheFn : String;
TheWidth : Integer;
a : Integer;
begin
Dbgrid1.Options := DBGrid1.Options - [DGTitles];
Headercontrol1.sections.Add;
Headercontrol1.Sections.Items[0].Width := 12;
For a := 1 to DBGRID1.Columns.Count do
begin
with DBGrid1.Columns.Items[ a - 1 ] do
begin
TheFn := FieldName;
TheCap := Title.Caption;
TheWidth := Width;
end;
With Headercontrol1.Sections DO
BEGIN
Add;
Items[a].Text := TheCap;
Items[a].Width := TheWidth + 1;
Items[a].MinWidth := TheWidth + 1;
Items[a].MaxWidth := TheWidth + 1;
END; (* WITH Headercontrol1.Sections *)
try (* except *)
{ Use indexes with the same name as the fieldname }
<I> (DataSource1.Dataset as TTable).IndexName := TheFn; { Try to set the index name }
</I> except
HeaderControl1.Sections.Items[a].AllowClick := False; { Index not Available }
end; (* EXCEPT *)
END; (* FOR *)
END; (* PROCEDURE *)</PRE>
<P><HR></P>
<P>Use the fieldname property of the DBGrid to set an index with the same name as the fieldname. </P>
<P><HR></P>
<PRE>
procedure TfrmDoc.HeaderControl1SectionClick(HeaderControl: THeaderControl;
Section: THeaderSection);
begin
<I> (DataSource1.Dataset as TTable).IndexName :=
DBGrid1.Columns.Items[ Section.Index - 1 ].FieldName;
</I>
end;</PRE>
<P><HR></P>
<P><H1><A NAME="dbgrid3">A Dbgrid with colored cells ?</P></A></H1>
<P><I>Ed_P._Hillmann@mail.amsinc.com (Ed Hillmann)</I></P>
I don't know if this helps, but I could color individual cells in a
DBGrid without having to make a new DBGrid component. This is what I
just tested.... <p>
I created a form, dropped a TTable component it, and pointed it to the
EMPLOYEE.DB database in the DBDEMOS database. I dropped a Datasource
and DBGrid on the form so that it showed on the form.<p>
I thought a simple test would be, for the employee number in the
EMPLOYEE.DB table, check if it's an odd number. If it's an odd
number, then turn that cell green.<p>
Then, the only code I attached was to the DBGrid's OnDrawColumnCell
event, which looks as follows....<p>
<HR><PRE>procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect:
TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
holdColor: TColor;
begin
holdColor := DBGrid1.Canvas.Brush.Color; {store the original color}
if Column.FieldName = 'EmpNo' then {only do for the cell displaying
EmpNo}
if (Column.Field.AsInteger mod 2 <> 0) then begin
DBGrid1.Canvas.Brush.Color := clGreen;
DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
DBGrid1.Canvas.Brush.Color := holdColor;
end;
end;
</PRE><HR>
This uses the DefaultDrawColumnCell method that is defined with the
TCustomDBGrid component, of which TDBGrid is a child. This turned
each cell green of an employee whose emp no was odd. <p>
<P><H1><A NAME="dbgrid4">DBGrid that shows images</P></A></H1>
<P><I>From: sraike@iconz.co.nz (Bill Raike)</I></P>
I've had second thoughts and decided to post my DBGrid descendant that
shows images, since it's such a small amount of code.<P>
Here it is: <P>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -