Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Can you help me change the below codes because stringgrid seem excu slowly



Permlink Replies: 2 - Last Post: Jun 15, 2017 1:28 AM Last Post By: Peter Below
hots wally

Posts: 53
Registered: 6/14/14
Can you help me change the below codes because stringgrid seem excu slowly
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 13, 2017 11:35 PM
dtpr1:=FormatDatetime('yyyy-mm-dd',DateTimePicker1.Date);
dtpr2:=FormatDatetime('yyyy-mm-dd',DateTimePicker2.Date);
kit:=Trim(Edit1.Text);
u_url:=allurl+'/show16_dquery.asp?zh='+fctuser+'&dtpr1='+dtpr1+'&dtpr2='+dtpr2+'&show=4&kit='+kit+'&t='+Form1.RandomValue(13);
resData := TStringStream.Create('',TEncoding.GetEncoding(65001));
reqData:=Tstringlist.Create;
try
Form1.NetHTTPClient1.Post(u_url, reqData, resData);
jsonstr:=resData.DataString.Trim;
finally
resData.Free;
reqData.Free;
end;
jo:=SO(jsonstr);
ja:=jo.A['text'];
i:=ja.Length;

try
sg2.RowCount := sg2.FixedRows + i;
for i := sg2.FixedRows to sg2.RowCount-1 do
begin
jb:=ja.O[i-1];
for j := 0 to 12 do
sg2.Cells[j, i] := jb.S[FieldNames4[j]];
end;
except
end;
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Can you help me change the below codes because stringgrid seem excu slowly
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 14, 2017 11:07 AM   in response to: hots wally in response to: hots wally
hots wally wrote:

stringgrid seem excu slowly

Did you profile the code to make sure the TStringGrid is the actual
bottleneck?

Given the code you have shown, there is not much you can do to speed it
up. The only thing I can recomend is try using the Rows[] property to
update each row at a time, instead of using the Cells[] property to
update individual strings at a time. Do as much work outside of the UI
as you can, eg:

jo := SO(jsonstr);
ja := jo.A['text'];
i := ja.Length;
 
row := TStringList.Create;
try
  for j := 0 to 12 do
    row.Add('');
 
  try
    sg2.RowCount := sg2.FixedRows + i;
    for i := 0 to i-1 do
    begin
      jb := ja.O[i];
      for j := 0 to 12 do
        row.Strings[j] := jb.S[FieldNames4[j]];
      sg2.Rows[sg2.FixedRows+i] := row;
    end;
  except
  end;
finally
  row.Free;
end;


Otherwise, consider switching to a different UI component, like a
virtual TListView in Report mode, or a TVirtualStringTree. Either of
those should be a lot faster (and nicer looking) than using TStringGrid.

resData := TStringStream.Create('',TEncoding.GetEncoding(65001));

Codepage 65001 is CP_UTF8, so you should use TEncoding.UTF8 instead of
creating a new TEncoding object:

resData := TStringStream.Create('', TEncoding.UTF8, False);


However, you don't actually need to use a TStringStream at all. Post()
returns an IHTTPResponse interface, which has a ContentAsString()
method:

jsonstr := Form1.NetHTTPClient1.Post(u_url,
reqData).ContentAsString.Trim;


--
Remy Lebeau (TeamB)
Peter Below

Posts: 1,227
Registered: 12/16/99
Re: Can you help me change the below codes because stringgrid seem excu slowly
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 15, 2017 1:28 AM   in response to: hots wally in response to: hots wally
hots wally wrote:

dtpr1:=FormatDatetime('yyyy-mm-dd',DateTimePicker1.Date);
dtpr2:=FormatDatetime('yyyy-mm-dd',DateTimePicker2.Date);
kit:=Trim(Edit1.Text);
u_url:
=allurl+'/show16_dquery.asp?zh='+fctuser+'&dtpr1='+dtpr1+'&dtpr2='+dtp
r2+'&show=4&kit='+kit+'&t='+Form1.RandomValue(13); resData :=
TStringStream.Create('',TEncoding.GetEncoding(65001));
reqData:=Tstringlist.Create; try Form1.NetHTTPClient1.Post(u_url,
reqData, resData); jsonstr:=resData.DataString.Trim;
finally
resData.Free;
reqData.Free;
end;
jo:=SO(jsonstr);
ja:=jo.A['text'];
i:=ja.Length;

try
try
sg2.rows[0].BeginUpdate;
sg2.RowCount := sg2.FixedRows + i;
for i := sg2.FixedRows to sg2.RowCount-1 do
begin
jb:=ja.O[i-1];
for j := 0 to 12 do
sg2.Cells[j, i] := jb.S[FieldNames4[j]];
end;
finally
sg2.rows[0].EndUpdate;
end;

This blocks redrawing of the grid while you change its content and
should speed things up a bit.


except
end;


--
Peter Below
TeamB

Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02