Jump to content


Photo

From dataset to AddCells()?


  • Please log in to reply
3 replies to this topic

#1 littlebigfred

littlebigfred
  • Members
  • 176 posts

Posted 15 July 2009 - 03:06 PM

Hello

This is most likely a newbie question: In the NextGrid HOWTO, I see that it has a AddCells() method which apparently fills the grid in one go, so as to avoid calling AddRows() followed by a double For loop (to go through each row, and each column).

I'd like to know how to use this method when the data is in a dataset:

CODE
//Create StringList to host array "SQL name = User-friendly name", and add columns
MySL := TStringList.Create;
MySL.CommaText := 'id=Identification, label=Label';
for index := 0 to MySL.Count - 1 do begin
  NextGrid1.Columns.Add(TNxTextColumn,MySL.ValueFromIndex[index]);
end;

With ASQLite3Query1 do begin
  SQL.Text := 'SELECT id,label FROM mytable';
  Open;

  //How to fill grid with AddCells?
  //E2250 There is no overloaded version of 'AddCells' that can be called with these arguments
  NextGrid1.AddCells(ASQLite3Query1.Fields);
end;

FreeAndNil(MySL);


Thank you.

#2 littlebigfred

littlebigfred
  • Members
  • 176 posts

Posted 16 July 2009 - 11:04 AM

Apparently, there's no way to use the AddCells() from a dataset, and we have to loop through each row, and within each row, through all columns, and use Cells[]:

CODE
    SQL.Text := 'SELECT id,label FROM mytable';
    Open;

    //Fill grid with stuff from dataset
    NextGrid1.AddRow(ASQLite3Query1.RecordCount);
    row := 0;
    First;
    while not EOF do begin
      for index := 0 to FieldCount - 1 do begin
        NextGrid1.Cells[index,row] := Fields[index].AsString;
      end;
      inc(row);
      Next;
    end;

    //Change grid look
    With NextGrid1 do begin
      //Important: Call BestFitColumn() before setting other options...
      for index := 0 to Columns.Count - 1 do begin
        BestFitColumn(index, bfBoth);
      end;
      Columns[Columns.Count - 1].Options := Columns[Columns.Count - 1].Options + [coAutoSize];
      Options := Options + [goSelectFullRow,goMultiSelect];
    end;


#3 Boki (Berg)

Boki (Berg)

    Boki (Berg)

  • Forum Admin
  • PipPipPipPipPip
  • 8,196 posts
  • Gender:Male

Posted 16 July 2009 - 08:54 PM

Hello Fred,

I will write one small demo for you tomorow. I hope that it will help you.

Best regards
boki@bergsoft.net | LinkedIn Profile
--
BergSoft Home Page: www.bergsoft.net
Users Section: users.bergsoft.net
Articles and Tutorials: help.bergsoft.net (Developers Network)
--
BergSoft Facebook page
--
Send us applications made with our components and we will submit them on: www.bergsoft.net/apps.htm. Link to this page will be also set on home page too.

#4 littlebigfred

littlebigfred
  • Members
  • 176 posts

Posted 16 July 2009 - 09:56 PM

QUOTE (Boki (Berg) @ Jul 16 2009, 09:54 PM) <{POST_SNAPBACK}>
I will write one small demo for you tomorow. I hope that it will help you.


Thank you, it's nice of you. The above code does work, but since AddCells looks a lot easier, I was curious to see if I could get it to work.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users