Discussion:
Somebody alive ?
(too old to reply)
Stark
2012-03-17 17:08:21 UTC
Permalink
If somebody is still visiting here, I would like to raise a question. I have
found on the net a beautiful example on how to make a professional looking
StringGrid. I have taken this example and I am trying to adapt it to my
needs. First of all, I need to fill the grid with data from a table.
In a normal situation I would have done something like (to simplify, suppose
1 column only):
while not Table1.Eof do
begin
GridOwnerDraw.Cells[row,col]:= GetText(Row,0);
inc(row);
Table1.next;
end;
In this case, the example is using a text string built inside the StringGrid
OnDraw Cell event. and the row is draw in the On Draw event with:
DrawText(gridOwnerDraw.canvas.Handle, PChar(str), length(str), txtRect,
DT_SINGLELINE or DT_LEFT or DT_VCENTER or DT_END_ELLIPSIS);
where str contains to text to be written in that cell.

I can't find a way to give the current field value to the DrawText . Any
suggestion ?
Jamie
2012-03-17 22:16:58 UTC
Permalink
Post by Stark
If somebody is still visiting here, I would like to raise a question. I
have found on the net a beautiful example on how to make a professional
looking StringGrid. I have taken this example and I am trying to adapt
it to my needs. First of all, I need to fill the grid with data from a
table.
In a normal situation I would have done something like (to simplify,
while not Table1.Eof do
begin
GridOwnerDraw.Cells[row,col]:= GetText(Row,0);
inc(row);
Table1.next;
end;
In this case, the example is using a text string built inside the
StringGrid OnDraw Cell event. and the row is draw in the On Draw event
DrawText(gridOwnerDraw.canvas.Handle, PChar(str), length(str),
txtRect, DT_SINGLELINE or DT_LEFT or DT_VCENTER or DT_END_ELLIPSIS);
where str contains to text to be written in that cell.
I can't find a way to give the current field value to the DrawText . Any
suggestion ?
not sure what you mean by current field value? DO you mean converting a
numerical type variable to a string? If so, IntToStr, FloatTostr etc.



If that isn't what you mean, please post an example of view .

Jamie
Stark
2012-03-18 16:30:27 UTC
Permalink
Post by Jamie
not sure what you mean by current field value? DO you mean converting a
numerical type variable to a string? If so, IntToStr, FloatTostr etc.
Sorry for my english. No, I meant: how do I feed the StringGrid with data
coming from a dataset ? I am reading records from a dataset and would like
to represent data coming from it (three fields) in each row of the
StringGrid. The OnDrawCell event has the following statement to draw the
selection:
DrawText(gridOwnerDraw.canvas.Handle, PChar(str), length(str), txtRect,
DT_SINGLELINE or DT_LEFT or DT_VCENTER or DT_END_ELLIPSIS);
where 'str' is the specific value to put in the specific cell. I need to
replace 'str' with the value coming from the first field of my record, then
the second etc. and then start a new row and paint this next row with the
fields coming from the second record and so on.
I hope I was clear enough. Or not ?
Jamie
2012-03-18 19:14:14 UTC
Permalink
Post by Stark
Post by Jamie
not sure what you mean by current field value? DO you mean converting a
numerical type variable to a string? If so, IntToStr, FloatTostr etc.
Sorry for my english. No, I meant: how do I feed the StringGrid with
data coming from a dataset ? I am reading records from a dataset and
would like to represent data coming from it (three fields) in each row
of the StringGrid. The OnDrawCell event has the following statement to
DrawText(gridOwnerDraw.canvas.Handle, PChar(str), length(str),
txtRect, DT_SINGLELINE or DT_LEFT or DT_VCENTER or DT_END_ELLIPSIS);
where 'str' is the specific value to put in the specific cell. I need to
replace 'str' with the value coming from the first field of my record,
then the second etc. and then start a new row and paint this next row
with the fields coming from the second record and so on.
I hope I was clear enough. Or not ?
yes,
you must populate the CELLS property with your DataSet information
before you allow the TStringGrid to draw

When the OnDraw event takes place in the TstringGrid, Build a string
locally in that event code block.
For example.

Var
Ls:String;

Begin
Ls := (Sender as TstringGrid).Cells[col, row];

// then you can use LS as the string in the windows API call.
End;

When ever the main class calls a member of one of its sub classes
like you have for example, it normally sends the "self" parameter to
be used in the OnDrawCell for example, for the Sender parameter. This
way, we know who sent it :)

The "AS" operator in delphi does run time checking to make sure the
Sender variable does represent a "TstringGrid" class and also allows
type casting to let you gain access to its members.

For example:

Code found in the Main class of the TstringGrid.

If Assigned(OnDrawCell) Then OnDrawCell(Self,.......)

so this means that the "Sender" variable will be that of the Instant
variable of your TstringGrid you created and the run time uses this with
the "AS" to check a against it.

You could also do this.

With Sender as TstringGrid do
Begin
Ls := Cells[col,row];
..

End;

P.S.
Unless they have changed things, I wouldn't try to get the string
to directly interact in the API DRAWText.. The string needs to be
transferred to a permanent location from the class to be
used as a string in a API call, which in this case would be the "LS"

I hope this is what you were looking for.

Jamie
Stark
2012-03-19 19:35:03 UTC
Permalink
I am out today, but I read your message. Tomorrow, I'll think about what you
wrote and try your suggestion. Thanks for the moment.
Stark
2012-03-21 17:55:29 UTC
Permalink
I just wanted to inform Jamie that his suggestion works perfectly.
Jamie
2012-03-21 22:57:01 UTC
Permalink
Post by Stark
I just wanted to inform Jamie that his suggestion works perfectly.
No problem..

Jamie

S.G
2012-03-19 22:48:11 UTC
Permalink
Post by Stark
I am reading records from a dataset and
would like to represent data coming from it (three fields) in each row
of the StringGrid. The OnDrawCell event has the following statement to
..
Post by Stark
I hope I was clear enough. Or not ?
I do not understand at all what exactly are you trying to achieve.

For instance, why even try to do something with Datasets + TStringGrid
combination? When there is a standard TDBGrid component to do all the
work for you.

What is the specific action you can't do with TDBGrid but you think you
can do with TStringGrid?

SG
Stark
2012-03-20 10:22:04 UTC
Permalink
Post by S.G
I do not understand at all what exactly are you trying to achieve.
For instance, why even try to do something with Datasets + TStringGrid
combination? When there is a standard TDBGrid component to do all the work
for you.
What is the specific action you can't do with TDBGrid but you think you
can do with TStringGrid?
SG
Yes, you are right. I don't need any specific action that can't be done with
a TDBGrid. My only purpose was to show a much nicer grid and I started from
an example found on the web which I was trying to adapt.
But then, when is it the case for using a StringGrid ?
Maarten Wiltink
2012-03-19 22:44:27 UTC
Permalink
Post by Stark
If somebody is still visiting here, I would like to raise a question.
I have found on the net a beautiful example on how to make a
professional looking StringGrid. I have taken this example and I am
trying to adapt it to my needs. First of all, I need to fill the grid
with data from a table.
<rattles chains>

I must say it looks decidely odd to see 'professional looking' and
'stringgrid' that close together without a negation to link them.
But let's not dwell on that.
Post by Stark
In a normal situation I would have done something like (to simplify,
while not Table1.Eof do
begin
GridOwnerDraw.Cells[row,col]:= GetText(Row,0);
inc(row);
Table1.next;
end;
Alright.
Post by Stark
In this case, the example is using a text string built inside the
StringGrid OnDraw Cell event. and the row is draw in the On Draw
DrawText(gridOwnerDraw.canvas.Handle, PChar(str), length(str),
txtRect, DT_SINGLELINE or DT_LEFT or DT_VCENTER or DT_END_ELLIPSIS);
where str contains to text to be written in that cell.
I can't find a way to give the current field value to the DrawText . Any
suggestion ?
The OnDraw event works the other way around. You don't push data to it,
it fires in its own time and it draws in the information it needs.

So we don't need to see just the code you think to write in the handler,
but (lacking the help file) also the parameters to the event. They should
tell you which cell the grid wants to draw, which row and column, and
from that you can deduce what information should go there. From the grid
row you should be able to figure out which record in your dataset you need
to seek to, and from the grid column which field value in the record.

You can avoid all the seeking by preloading the query result into memory.
Obviously, when there are a million rows, that idea needs refining.

Depending on your Delphi edition, you might well have a data-aware grid
available to you.

Groetjes,
Maarten Wiltink
Loading...