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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OnDrawCell Error for TStringGrid

Status
Not open for further replies.

PenCup

Programmer
May 5, 2009
5
0
0
US
Hello, this is my first post here. I have not found information on this topic using the forum search. I am using a custom onDrawCell event to change the color of cells in a TStringGrid. I have a function which is called to update the table called refreshList(). The function sets the cell data to a string for each row in the table, then calls grid->repaint.

I have my code for the onDrawCell event and refreshList function in a try block, with the catch section set to inform me of a failure. Both of these functions fail from time to time. It seems almost random as there will be a period of time with no errors, then a period where there are many errors. The data in the TStringGrid seems to jumble at this time, by this I mean data from cell 1 is in cell 2 and so on, in no particular fashion.

Does anyone have any suggestions for this problem or has anyone seen this before? It is baffling me at the moment. I would also like to post the code for these two functions, would someone tell me how this is done.

Thanks all for any help.
 
Hi and welcome. Please post your OnDrawCell event method, and the RefreshList method. Make sure you enclose your code in [ignore]
Code:
[/ignore] tags
 
Here is the refreshList method:

Code:
HRESULT __fastcall TPSM_Form1::RefreshList()
{
 try{
        EXVECT::iterator itrE;
        itrE = LogList.end()-1;
        Count->Caption = IntToStr(LogList.size());

       for(int Row = 1; Row != LogList.size()+1; Row++)
       {
                RTExGrid->Cells[0][Row] = "  +";
                //Date
                RTExGrid->Cells[1][Row] = Trim(FormatDateTime("mm/dd/yyyy",(*itrE)->Exc.StartDateTime));
                //Time
                RTExGrid->Cells[2][Row] = Trim(FormatDateTime("HH:nn:ss",(*itrE)->Exc.StartDateTime));
                //test
                RTExGrid->Cells[3][Row] = "000";
                RTExGrid->Cells[4][Row] = "R#";
                RTExGrid->Cells[5][Row] = "A#";
                RTExGrid->Cells[6][Row] = "T#";
                RTExGrid->Cells[7][Row] = "$0.00";
                RTExGrid->Cells[8][Row] = "E#";

                if(itrE == LogList.begin())
                        break;
                itrE--;
       }
       RTExGrid->Repaint();
       return S_OK;
 }
 catch(...)
 {
        WriteLog(L_ERROR, "RefreshExceptionList has FAILED");
 }
}

The method is meant to scroll an iterator through a list of data and post information to the grid, for now I have most cells filled with test Strings.

the OnDrawCell method:

Code:
void __fastcall TPSM_Form1::RTExGridDrawCell(TObject *Sender,int ACol, int ARow,TRect &Rect, TGridDrawState State)
{

try{

        TStringGrid* StringGrid =
    static_cast<TStringGrid*>(Sender);
  assert(StringGrid != NULL);

  TCanvas* SGCanvas =StringGrid->Canvas;
  SGCanvas->Font = StringGrid->Font;

  RECT RText = static_cast<RECT>(Rect);
  const AnsiString text(
    StringGrid->Cells[ACol][ARow]);

  const bool fixed = 
    State.Contains(gdFixed);
  const bool focused = 
    State.Contains(gdFocused);
  bool selected = 
    State.Contains(gdSelected);
  if (!StringGrid->Options.Contains(
    goDrawFocusSelected)) {
    selected = selected && !focused;
  }
  // if the cell is fixed (headers)
  if (fixed) {
    SGCanvas->Brush->Color = 
      StringGrid->FixedColor;
    SGCanvas->Font->Color = clBtnText;
    SGCanvas->FillRect(Rect);
    Frame3D(SGCanvas, Rect, 
      clBtnHighlight, clBtnShadow, 1);
  }
  // if the cell is selected
  else if (selected) {
    SGCanvas->Brush->Color =clHighlight;
    SGCanvas->Font->Color = 
      clHighlightText;
    SGCanvas->FillRect(Rect);
  }
  // if the cell is normal
  else {
    SGCanvas->Brush->Color =
      StringGrid->Color;
    SGCanvas->Font->Color = 
      StringGrid->Font->Color;
    SGCanvas->FillRect(Rect);
  }
  // if the cell is focused
  if (focused) {
    DrawFocusRect(
      SGCanvas->Handle, &RText);
  }

  // draw the text
  RText.left += 2; RText.top += 2;
  DrawText(SGCanvas->Handle,
    text.c_str(), text.Length(), &RText,
    DT_LEFT |DT_VCENTER |DT_SINGLELINE);

    }
    catch(...)
    {
        WriteLog(L_ERROR, "RTExGrid DrawCell Event FAILED.");
    }
}

This code is just meant to draw the cell colors. I read that there may be more to add because goRowSelect property of the grid is set to true, but I am not sure what else is needed for that.
Thanks again.
 
That looks like C, not Delphi. I think you're in the wrong forum...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top