How to turn a StringGrid component into a DBStringGrid so that it works without problems with MS SQL Server?

It is known that StringGrid allows StringGrid to access the value of each cell using for i...do , but the DBGrid component cannot. Question: how to add a property in StringGrid to connect to the database table of MS SQL Server 2008? I really hope for your hint. I work in Delphi 7.

Object Table TStringGrid Property ColCount = 2 The procedure for pressing the "Calculate" button on a form is something like this:

 procedure TForm1.BitBtn1Click(Sender: TObject); var i, j, neis, rez : Integer; tmp : Currency; begin q := 0; for i := 1 to Table.RowCount -1 do begin for j := 1 to Table.ColCount -1 do begin if Table.Cells[j, i] = '' then Table.Cells[j, i] := '0'; Application.ProcessMessages; end; end; for i := 1 to Table2.RowCount -1 do begin for j := 1 to Table2.ColCount -1 do begin if Table2.Cells[j, i] = '' then Table2.Cells[j, i] := '0'; Application.ProcessMessages; sleep(10); end; end; 
  • Why can't you loop through a dbgrid? - Nofate

3 answers 3

Are you a magician to transform something? Take data from the DataSet , parse it with pens and write in StringGrid . But do you need it?

And you can access the data in each cell through the same DataSet .

ADDED

All the boron cheese is due to the fact that it is necessary to replace empty cells with 0. Sadly, really, at the design stage of the base architecture, it was impossible to assign a default value to these fields? Yes, even now, no one bothers you to do it. In the extreme case, you can make a request so that before sampling on the server side, a 0 is written to the table instead of an empty field. No, we prefer to pervert over a StringGrid ohm, especially a cycle, go through the table twice, that is, if there are 10 thousand records, then it will be terrible to brake.

    StringGrid and DBGrid are data presentation components. To work with DELPHI, other components exist, in which the obtained results are found after the query and you can work with them there as with arrays) You would rather say that you ultimately need it - perhaps all this is solved using standard components and reinventing the wheel it will not be necessary)

    • I would like to display the database table from the server in the grid, and then process (calculate) by the algorithm, using the values ​​in its cells (for i ... do ...). Moreover, the calculation algorithm is performed by clicking on the button form Delphi application. - alexstas7
    • From the above, I see 2 ways: 1. It is possible to make the calculation algorithm on the database server - that is, make up a SQL query that satisfies your calculation algorithm. 2. If the 1 point is impossible - then work with the DataSet component as a two-dimensional array. How do you connect to the database? What component do you use? - Ale_x
    • Yes. You are right - the server is the best to handle. But here's the catch - I don't know how to transfer this calculation to the SQL query. - alexstas7
    • A table in the studio (field) and what to count) - Ale_x
    • Object Table TStringGrid <br> Property ColCount = 2 The procedure when you click the "Calculation" button on a form is something like this: procedure TForm1.BitBtn1Click (Sender: TObject); var i, j, neis, rez: Integer; tmp: Currency; ini1, ini2: TIniFile; begin q: = 0; for i: = 1 to Table.RowCount -1 do begin for j: = 1 to Table.ColCount -1 do begin if Table.Cells [j, i] = '' then Table.Cells [j, i]: = ' 0 '; Application.ProcessMessages; sleep (30); end; end; - alexstas7

    If you just need to display zeroes instead of emptiness, then as one of the options, define an OnDrawDataCell handler for the DBGrid. And do not invent extra hemorrhoids with TStringGrid:

     procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect; Field: TField; State: TGridDrawState); begin if Field.IsNull then DBGrid1.Canvas.TextOut(Rect.Left + 5, Rect.Top, '0'); end; 
    • Thank you very much! I'll try. - alexstas7