Hello, I am a new user to borland and I am getting some errors with displaying data in my TStringGrid. Sometimes it works well, and then other times all the cells seem to get mixed up randomly. I notice that my catch block outputs that 2 sections of my code are failing, a method to write some data to the grid, and the overridden method of OnDrawCell. I am not really sure what is causing the error as the code does not do much in those 2 sections. Has anyone heard of a problem like this before, and perhaps know a way to solve it? Thanks for your input.
Method RefreshList: iterate through a list and display test data in the grid, then call repaint.
Method onDrawCelll: draw the headers and normal cells differently.
Method RefreshList: iterate through a list and display test data in the grid, then call repaint.
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, "RefreshList has FAILED");
}
}
Method onDrawCelll: draw the headers and normal cells differently.
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.");
}
}