StringGrid question.

Hi.

When i open my application 1st stringgrid cell is always selected.
How can i avoid that ?

Thanks in advance.
LVL 16
CodedKAsked:
Who is Participating?
 
Russell LibbyConnect With a Mentor Software Engineer, Advisory Commented:

Try the following  code, if it does not work then please post a simple example that reproduces the problem along with the dfm listing

Russell

---

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Grids;

type
  TForm1            =  class(TForm)
     StringGrid1:   TStringGrid;
     procedure      StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
     procedure      FormCreate(Sender: TObject);
     procedure      StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean);
  private
     // Private declarations
     procedure      UpdateEditor;
  public
     // Public declarations
  end;

type
  TSuperCustomGrid  =  class(TCustomGrid); // We need to access the protected props

var
  Form1: TForm1;

implementation
{$R *.DFM}

procedure TForm1.UpdateEditor;
begin

  // Update editor if assigned
  if Assigned(TSuperCustomGrid(StringGrid1).InPlaceEditor) then
  begin
     if (StringGrid1.Row mod 2) = 0 then
        TWinControl(TSuperCustomGrid(StringGrid1).InPlaceEditor).Brush.Color:=RGB(226,226,226)
     else
        TWinControl(TSuperCustomGrid(StringGrid1).InPlaceEditor).Brush.Color:=RGB(207,241,87);
  end;

end;

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
begin

  // Check row
  if (ARow mod 2) = 0 then
     StringGrid1.Canvas.Brush.Color:=RGB(226,226,226)
  else
     StringGrid1.Canvas.Brush.Color:=RGB(207,241,87);
  // Clear rect
  StringGrid1.Canvas.FillRect(Rect);
  // Make sure color is not changed if selected
  StringGrid1.Canvas.Font.Color:=StringGrid1.Font.Color;
  // Draw text
  StringGrid1.Canvas.TextRect(Rect, Rect.Left, Rect.Top, StringGrid1.Cells[ACol, ARow]);

end;

procedure TForm1.FormCreate(Sender: TObject);
var  dwRows, dwCols:      Integer;
begin

  // Update editor
  UpdateEditor;

  // Add some sample data
  for dwRows:=0 to Pred(StringGrid1.RowCount) do
  begin
     for dwCols:=0 to Pred(StringGrid1.ColCount) do
        StringGrid1.Cells[dwCols, dwRows]:=Format('%d%d', [dwRows, dwCols]);
  end;
 
end;

procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean);
begin

  // Update editor if needed
  UpdateEditor;

end;

end.
0
 
calinutzCommented:
Focus another control on the form...
something like
Button1.SetFocus;
0
 
CodedKAuthor Commented:
Hi calinutz.
This doesnt work !

The problem is that i have painted the cells with some colors i like...
If a cell is focused then the focused cell font color is painted white
and the user cant read anything.

0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
CodedKAuthor Commented:
Another problem... there isnt any control on this form to focus...
I tried to set the focus on the form. Didnt work.
0
 
Russell LibbySoftware Engineer, Advisory Commented:

Could we see the code for the painting routine (guessing you are using the OnDrawCell event)?

Russell
0
 
CodedKAuthor Commented:
Hi Russell.

On StringGrid1DrawCell

If (ARow mod 2)=0 then
  begin
  StringGrid1.Canvas.Brush.Color:= RGB(226,226,226);//RGB(245,147,51);
  StringGrid1.Canvas.TextRect(Rect, Rect.Left, Rect.Top, StringGrid1.Cells[ACol, ARow]);
  end else
  begin
  StringGrid1.Canvas.Brush.Color:= RGB(207,241,87);//RGB(156,241,93);
  StringGrid1.Canvas.TextRect(Rect, Rect.Left, Rect.Top, StringGrid1.Cells[ACol, ARow]);
  end;
0
 
Russell LibbySoftware Engineer, Advisory Commented:

The problem is that with the OnDrawCell event is skipped for the cell that has edit focus. (eg you might have  [goEditing, goAlwaysShowEditor] set). Also keep in mind that the cell with edit focus is actually displaying an edit control over the grid cell,
so even if you could paint the cell it would not do you any good, as there is an edit control over it. I am looking at a way to paint the inplace editor, but you may want to consider changing the font color (knowing that you will be dealing with an edit contol)

Russell
0
 
CodedKAuthor Commented:
[goEditing, goAlwaysShowEditor] are set to false.
User wont edit anything... Font color is set to black, i've tried blue.... but what ever font color i choose
there is always a row selected with white non readable letters. :/

Thank you :)
0
 
Russell LibbySoftware Engineer, Advisory Commented:
Almost have the editor super classed and painted, let you know in a minute.

Russell
0
 
CodedKAuthor Commented:
:)
0
 
CodedKAuthor Commented:
Thank you very much Russell :)
:) :)
0
 
CodedKAuthor Commented:
200 -> 500 :)
0
 
Russell LibbySoftware Engineer, Advisory Commented:
Thank you as well. Its working as expected for you, I hope?

Russell
0
 
CodedKAuthor Commented:
Yes perfect...

I only changed this :

type
  TSuperCustomGrid  =  class(TCustomGrid);

to this :

type
  TSuperCustomGrid  =  class(TCustomGrid)
end;

Dont know why it didnt work without this change :/
0
 
CodedKAuthor Commented:
Nop it works as it is... My mistake... I had multiple errors and did some changes ...
Code is perfect.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.