Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stringgrid Problem 4

Status
Not open for further replies.

abx5

Programmer
Aug 23, 2006
10
TH
Hi,

I'm working on the Stringgrid table by pulling the data from Database and put into StringGrid. Doing that at first is fine but when I want to add some data from InputBox command into the same StringGrid. No data that I just added is showing at all. Also, by using SHOWMESSAGE command to check data in that cell on the Stringgrid, the data is there but just not showing.
My question is that this is the normal behavior for Stringgrid or what command I can do to refresh the data? (I already tried command REFRESH, REALIGN, REPAINT but none of them work.) I'm using Delphi 2006.

Thank you,
 
Are you sure you are not using the Database version of Stringgrid (DbGrid). If not consider using this instead its optomised for use with Db's


Steve: Delphi a feersum engin indeed.
 
I use Stringgrid, not DBgrid. The reason I go for Stringgrid is that I don't want to have any problem with record lock something like that. (which I have a lot in the past with Paradox.) So, the connection to the database is only when needed and free it after that. Right now, mySQL is what I use.

Also, I've read long time ago that Stringgrid allows me to make some modification more than what DBGrid provided. Can you confirm me if this is true or not?

Thank you,



 

I always use TStringGrid instead of the DB aware components for exactly the same reason.

But I don't understand the problem you are having. Are you overwriting data in cells or putting in new data? You must have increased the row count or you would be getting errors. Can you provide more info on what you are actually doing?

 
If it's a brand new row, have you remembered to increased the rowcount to accommodate the new record?

www.radbmx.co.uk
 
I just want to add new data into new row. I use Inputbox/InputQuery to get data, then, try to fill that data into a new row in Stringgrid. (This Stringgrid already had some rows from database before.) I did use the command

Stringgrid.RowCount:=StringGrid.RowCount+1;

The new row shows up but when I use the command like

Stringgrid.Cell[Col,Row]:='abc';

The data is not there. I even tried to use SHOWMESSAGE to check what's the data is really in there. It shows correctly though. The only problem is the data that should be there is not showing at all. It's a new row with blank data in it.
 
Don't know what the problem exactly is (show us some actual code). here is some code I use to fill my stringgrid from a db query :

Code:
procedure TFrm_criteria.RefreshGrid;

var RowIndex : integer;
    ColIndex : integer;

begin
 with Qry_sites do
  begin
   StringGrid.RowCount:=RecordCount+1;
   if StringGrid.RowCount = 1 then
    begin
     // fixedrows must be smaller than Rowcount
     // that's why I have to include one extra row, filled with text 'no data'
     StringGrid.RowCount:=2;
     StringGrid.FixedRows:=1;
     // clear cells
     for ColIndex:=1 to StringGrid.ColCount-1 do
      StringGrid.Cells[ColIndex, 1]:='';
     StringGrid.Cells[2,1]:='no data';
    end;
   First;
   if RecordCount > 0 then
    for RowIndex:=1 to RecordCount do
     begin
      StringGrid.Objects[0, RowIndex]:=Pointer(FieldByName(STR_FLD_ID).AsInteger);
      for ColIndex:=1 to StringGrid.ColCount-1 do
       begin
        StringGrid.Cells[ColIndex, RowIndex]:=FieldByName(CFields[ColIndex-1]).AsString;
       end;
      Next;
     end;
   First;
  end;
end;

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Hi,

I think I know where the problem is. Because I try to create another procedure to handle this thing, when working with Stringgrid in another procedure, it didn't work at all. (The command Stringgrid.RowCount:=StringGrid.Rowcount+1 didn't work also. But because I normally put it in the Button procedure, so, it works.)
So, it goes like this... When I click at the button, the procedure activated. This procedure ask for InputData. Then, it add a new row into Stringgrid. Then, call out another procedure.
The 2nd procedure is to save data into database and pull it back into StringGrid.
The problem lie on 2nd procedure. I merged the code from both procedure and it works fine now.
Right now, I will stick with only one procedure because I need to make the program done asap. However, I'm wondering what data may I need to pass from 1st procedure to 2nd procedure to make it work.
The second procedure is similar to below:



procedure TFrmTest.SaveCategory(CategoryName:String);
var ColNo, RowNo : Integer;
begin
Query1.SQL.text:= 'SELECT * FROM tbcategory ORDER BY IntCategoryID;';
Query1.Open;
Query1.Last;
ColNo:=1;
GridCategory.RowCount:=GridCategory.RowCount+1;
RowNo:=GridCategory.RowCount-1;
Showmessage(IntToStr(RowCountTmp));
GridCategory.cells[ColNo,RowNo]:=Query1.FieldByName('STRCategory').AsString;
ColNo:=ColNo+1;
GridCategory.Cells[ColNo,RowNo]:=Query1.FieldByName('DATECreate').AsString;
ShowMessage(GridCategory.Cells[ColNo,RowNo]);
ShowMessage(Query1.FieldByName('DATECreate').AsString);
ColNo:=ColNo+1;
GridCategory.Cells[ColNo,RowNo]:=Query1.FieldByName('INTCategoryID').AsString;
ShowMessage(Query1.FieldByName('INTCategoryID').AsString);
end;

 

The simplest way would be to make the second procedure part of TfrmTest as well. But if you can't do that for some reason, then all you need to do is add the TStringGrid to the parameter list for the second procedure.

I might do it something like this:

Code:
[COLOR=green]{Add a row to a grid and populate}[/color]
procedure AddToGrid( AGrid:TStringGrid;
               ACategory, ADate, ACatID:string );
var nRow: Integer;
begin
    with AGrid do
    begin
      nRow := RowCount;
      RowCount := RowCount+1;
      Cells[1,nRow] := ACategory;
      Cells[2,nRow] := ADate;
      Cells[3,nRow] := ACatID;
    end;
end;

[COLOR=green]{Retrieve category data and store in the grid.}[/color]
procedure TFrmTest.SaveCategory(CategoryName:String);
begin
    with Query1 do
    begin
      Close;
      SQL.text:= 
        'SELECT * FROM tbcategory ORDER BY IntCategoryID;';
      Open;
      Last;
      AddToGrid( GridCategory,
                 FieldByName('STRCategory').AsString,
                 FieldByName('DATECreate').AsString,
                 FieldByName('INTCategoryID').AsString );
      Close;
    end; 
end;


 

I just noticed that you are not using the string parameter "CategoryName" that is being passed into the procedure. I left it there anyway. Perhaps you will be using it in the future.

 
Zathras, your code works. I can make it now. Thanks a lot.
 
What database are you using, by the way, as your query may possibly be improved?

www.radbmx.co.uk
 
LicieLastic, I'm using MySQL 5.0. What do you mean by improving the query?
 
hi. I was hoping you were using SQLServer because this has the TOP keyword. Looking at your code, you are selecting everything (which could possibly get slower over time if table grows fast), ordering by IntCategoryID, then jumping to the last record.

Firstly, you could order descending with ORDER BY IntCategoryID desc and remove the .Last in your delphi code, this may save time, depends on size of table.

Fyi, in SQL Server, you could of had the following which would have returned 1 record, the record you wanted:

SELECT TOP 1 * FROM tbcategory ORDER BY IntCategoryID desc

Just thought you'd like to know [smile]

lou


www.radbmx.co.uk
 
You can limit the number of rows returned in a MySQL SELECT statement. Something like this should be appropriate:
Code:
SELECT * FROM tbcategory ORDER BY IntCategoryID DESC LIMIT 1
This would select the row with the highest value of IntCategoryID.

Andrew
Hampshire, UK
 
Good stuff, Andrew [smile] I don't know MySQL so was unaware of the LIMIT command.

www.radbmx.co.uk
 
LucieLastic, towerbase. Thanks a lot for your suggestion. I've tried it and it works great. I'm sure I will find time to look into the commands used in MySQL to know more about it.
 
I'm not sure that I deserve a star for that, but thanks anyway!

I think that MySQL is a great database and it's free for many situations. The Tek-Tips MySQL forum has two or three very clever people who seem to know both SQL and MySQL inside out and are really helpful. I've learned a lot from them.

Andrew
Hampshire, UK
 
some guys get all the stars :) [noevil]

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top