-1

I've created a string grid with a certain amount of columns and rows. I've also handle a right clic event on the string grid which displays a popup menu when you clic on the right button. You have some options inside this popup menu.

My question is how do i change background or police font color of a cell when i select an option from my popup menu. I know we can get the selected col using stringGrid.Col and same for the row, and i also know we ca change color on draw cell event. But i want to change the color on user action only.

For example, in my table i open up a file and i load the file content into a string grid (it's a CSV file). This file will be modified in my application from the string grid and then exported to a databse. An user can select a particular col with the right clic and then press primary key or foreign key or any other option. When he selects primary key, for example, i want to change the color of the column header so he can know which clumn is the primary key, which is the foreign key and so on. See what i mean?

PS: I am using delphi 2006 and can't change to another version.

PS: i've searched for a delphi forum on stackoverflow/exchange but didn't find the correct forum i guess

user28470
  • 419
  • 5
  • 17
  • Store the desired Columns in an array or a list and check in DrawCell if aCol is contained and aRow=0. – bummi Sep 19 '14 at 07:33
  • What are you struggling with? How to draw different cells in different colours? – David Heffernan Sep 19 '14 at 07:35
  • The problem with draw cell is that it fires when the cell is created and i don't want that. I want to change color only on user action after the whole string grid was already been rendered – user28470 Sep 19 '14 at 07:36

1 Answers1

1
  1. Paint the background in the desired colour in an OnDrawCell handler as you currently do.
  2. When you need to change colours, in response to user action, force a paint cycle by calling Invalidate on the grid.

If for some reason you don't want to invalidate the entire control, calculate the rectangle that needs to be re-painted and pass it to InvalidateRect.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 1
    You can make a cracked TStringGrid and call protected InvalidateCell as well. – LU RD Sep 19 '14 at 07:58
  • I don't have the invalidateRect option i have only Invalidate. Also, i just tried with invalidate. i have a bool value that is true when i call invalidate and my drawcell event look like this 'if primaryKeySelected then begin cell.text := sg.Celle[ACol, 0] sg.Canvas.brush.Color := clRed, sg.Canva.FillREct(Rect), textout .end' But it still colors the whole grid idk why – user28470 Sep 19 '14 at 08:00
  • You can call `InvalidateRect`. It's a windows API function. As LURD says, cracking the class to call protected `InvalidateCell` is probably a better option. – David Heffernan Sep 19 '14 at 08:01
  • How do i add IvalidateRect or Cell api?, it seems better than the simple draw cell. I never worked with delphi before. – user28470 Sep 19 '14 at 08:03
  • I thought you knew how to paint cell backgrounds already? It seems that you have not got that bit straight. Do you understand how painting works in Windows? WM_PAINT, InvalidateRect etc. – David Heffernan Sep 19 '14 at 08:04
  • Well i know how to paint the whole grid but idk how to use the InvalidateRect. I'm trying to find out now ^^ – user28470 Sep 19 '14 at 08:08
  • Your code should work with `Invalidate`. Concentrate on making that work first. Then worry about optimising. If your code won't paint right when you use `Invalidate` then it will never work. Do you understand how painting works in Windows? WM_PAINT, InvalidateRect etc. Do you understand the concept of valid/invalid regions? – David Heffernan Sep 19 '14 at 08:10
  • Ok i found out how to make it work with Invalidate, i defined my rect with sg.CellRect(sg.Col, 0); i just want to color the header. And it works great thanks :D. How can i invalidate only 1 rect now? As i get the Rect value from the drawcell event it was repainting the whole grid because of this. ^^ My grid contain right now more that 1000 rows, and invalidating the whole grid may be slow i think? – user28470 Sep 19 '14 at 08:20
  • Use the protected hack to gain access to `InvalidateCell`. http://hallvards.blogspot.co.uk/2004/05/hack-4-access-to-protected-methods.html – David Heffernan Sep 19 '14 at 08:21
  • Perfect! I defined TCustomGrid as a class of TStringGrid and then i'm calling it with With TCustomGrid(sg) do InvalidateCell(sg.Col, sgRow)! Thanks mate :D – user28470 Sep 19 '14 at 08:27
  • @user28470, I would not use TCustomGrid as the cracked class name. Easily confused with the parent of TStringGrid. – LU RD Sep 19 '14 at 08:31
  • Does Delphi has gaphical issue? When i'm scrolling horizontally the colored column header is like splashing all over other headers. – user28470 Sep 19 '14 at 09:08
  • The header name is splashe also :/ in my popupmenu i have `With tCustomStrinGrid(sg) do InvalidateCell(sg.Col, 0)` and then in my drawCell event `rec := sg.celRec(sg.col,0); cellText := sg.cells[sg.col,0];sg.canvas.font.style := [fsBold,fsUnderline]; sg.CanvasFillRect(rec) sg.canvas.textout(rec.Left +2, rec.top+2;cellText);` Something wrong? – user28470 Sep 19 '14 at 09:17
  • I know it's splashing on the headers cause we can see what's behind the header that's splashed on them – user28470 Sep 19 '14 at 09:23
  • I don't really understand what you are talking about. I've no idea what splashing on headers means. I tried to answer the question that you asked. I can't really debug code that I can't see that appears in comments. I think perhaps you need to understand that Stack Overflow is not a debugging service where we debug your programs for you. We try to answer questions that are asked. If you have more questions, ask more questions. – David Heffernan Sep 19 '14 at 10:00