TAdvStringGrid

Example 7 in C++Builder : Setting cell color, alignment & sorting styles

vcl grid

This project shows how to set cell colors (including cell data dependent colors), alignment in C++Builder as well as different sorting styles and also how to have different hints.

First of all, to populate the grid, data from a CSV file is used. In the FormCreate routine, this data is loaded. The column header text is set at design time, using the ColumnHeaders property. This is the code to load the data :

void __fastcall TForm1::FormCreate(TObject *Sender)
	{
	AdvStringGrid1->SaveFixedCells=FALSE;
	AdvStringGrid1->LoadFromCSV("sample.csv");
	AdvStringGrid1->AutoSizeColumns(FALSE,8);
	}
	
First, the SaveFixedCells property is set to FALSE, because the data for the fixed cells is not stored in the CSV file. This property controls whether this data is saved or loaded in the SaveToCSV, LoadFromCSV, SaveToFile, LoadFromFile functions. After loading the CSV file, the columns are autosized with the AutoSizeColumns method. The first parameter controls whether fixed columns are also sized. Since there are no fixed columns in this example, the first parameter makes no sense and is set to FALSE. The second parameter is the padding parameter and determines how much pixels to add to the width of the longest string in a column. This padding is especially useful to avoid that text from two consecutive columns are too close to each other and become unreadable as well as to make sure the sorting indicator (little arrow in column header) is always visible. The value of 8 makes sure there is always room left for the indicator. As an alternative, the EnhTextSize property can be used to automatically resize the string and use "..." at the end if the text does not fit.

Next, the OnGetCellColor event is used to set the cell color & font properties for each cell. This event handler is called for every cell and allows to choose different fonts, colors and font styles for each cell. In this example, only the color for the first column is changed and the color of the "Cylinder" column is changed depending on the value of the cell.

void __fastcall TForm1::AdvStringGrid1GetCellColor(TObject 
	*Sender, long ARow,
	long ACol, TGridDrawState AState, TBrush *ABrush, TFont *AFont)
	{
	if ((ACol==0) && (ARow>0))
	{
	ABrush->Color = clYellow;
	AFont->Color = clRed;
	AFont->Style << fsBold;
	}
	if ((ACol==2) && (ARow>0))
	{
	if (AdvStringGrid1->Cells[ACol][ARow]=="4")
	AFont->Color=clGreen;
	if (AdvStringGrid1->Cells[ACol][ARow]=="6")
	AFont->Color=clBlue;
	if (AdvStringGrid1->Cells[ACol][ARow]=="8")
	AFont->Color=clBlack;
	if (AdvStringGrid1->Cells[ACol][ARow]=="12")
	AFont->Color=clRed;
	}
	}
	
The text alignment is specified with the GetAlignement event handler. The default alignment is left justified, so only the columns that must be centered or right-justified are specified here :

void __fastcall TForm1::AdvStringGrid1GetAlignment(TObject 
	*Sender, long ARow,
	long ACol, TAlignment &AAlignment)
	
	switch(ACol) {
	case 1: AAlignment=taCenter;
	break;
	case 2,4: AAlignment=taRightJustify;
	break;
	}
	}
	
The TAdvStringGrid SortShow property is set TRUE to allow that users can click on the column header to sort columns. As the different columns have a different type of data and the sorting method needs to differ for each column, the OnGetFormat event handler is used to specify the data type of all columns. This is a simple switch construct, specifying the data type for each column. The style is set to any of the following values :

TSortStyle = (ssAutomatic, ssAlphabetic, ssNumeric, ssDate, 
	ssAlphaNoCase, ssAlphaCase, ssShortDateEU, ssShortDateUS, ssCustom, ssFinancial);
	
	
	void __fastcall TForm1::AdvStringGrid1GetFormat(TObject *Sender, long ACol,
	TSortStyle &AStyle, AnsiString &aPrefix, AnsiString
	&aSuffix)
	{
	switch(ACol) {
	case 0: AStyle=ssAlphaNoCase;
	break;
	case 1: AStyle=ssAlphaNoCase;
	break;
	case 2: AStyle=ssNumeric;
	break;
	case 3: AStyle=ssDate;
	break;
	case 4: AStyle=ssNumeric;
	aSuffix="pk";
	break;
	case 5: AStyle=ssNumeric;
	aPrefix="$";
	break;
	}
	}
Notice how the aSuffix and aPrefix parameters are used in numeric sorting to have proper numeric sorts for cell data that contains data other than numbers. If you need an even more complex sorting method, specify the ssCustom style. In this case, the OnCustomCompare event will be called with the data that must be compared as parameters and the application must return 0 (if equal) ,-1 (if s1<s2) or 1 (if s1>s2) depending on sort result.

As a last extension, the hinting of the grid is changed for each column. :

void __fastcall TForm1::AdvStringGrid1GridHint(TObject *Sender, 
	long Arow,
	long Acol, AnsiString &hintstr)
	{
	switch(Acol) {
	case 0: hintstr="Car manufacturer";
	break;
	case 1: hintstr="Car model";
	break;
	case 2: hintstr="Nr. of cylinders in model";
	break;
	case 3: hintstr="Introduction date";
	break;
	case 4: hintstr="Engine horse power";
	break;
	case 5: hintstr="Date of production start";
	break;
	case 6: hintstr="Link to manufacturer website";
	break;
	}
	}
	
Project & source files for downloading : CASGSAMPLES.ZIP