What is a StringGrid or TStringGrid in C++ Builder? How can I display vector values visually? How can I display a one-dimensional vector in a string grid component? How can I use Cells in StringGrid? How can I display a two-dimensional vector in a StringGrid component? Can I use std::vectors in C++ Builder? Let’s answer these questions.
C++ Builder is a c++ software that has a lot of components to display your data visually in your desktop and mobile applications. One of the grid based component is StringGrid (TStringGrid) that allows you to list your values in a grid like an excel table. StringGrids are easy to use in both VCL and FMX applications. In this post, we will explain how you can list vector values with a StringGrid component in Windows C++ Applications.
Table of Contents
What is a StringGrid in C++ Builder?
A TStringGrid represents a grid control designed to simplify the handling of strings and associated objects. Add a TStringGrid object to a form to present textual data in a tabular format. TStringGrid provides many properties to control the appearance of the grid, as well as events and methods that take advantage of the tabular organization of the grid in responding to user actions.
TStringGrid introduces the ability to associate an object with each string in the grid. These objects can encapsulate any information or behavior represented by the strings that are presented to the user.
For a TStringGrid, the DrawingStyle property controls whether the current settings have any effect for FixedColor, and for GradientStartColor and GradientEndColor :
- Setting FixedColor has no effect unless you also set DrawingStyle to
gdsClassic
. - Setting GradientStartColor and GradientEndColor has no effect unless you set the value of DrawingStyle to
gdsGradient
.
Note that, If the strings to be presented in a grid represent field values from the records in a dataset, use TDBGrid instead. Also, to display the string and the associated object or control, use TDrawGrid instead. .
What is the purpose of the StringGrid Cells property in C++ Builder?
Cells are the text containers of the StringGrid. The Cells property lists the strings for each cell in the grid.
Syntax:
1 2 3 |
__property System::UnicodeString Cells[int ACol][int ARow] = {read=GetCells, write=SetCells}; |
For example we can define a 2×2 StringGrid table and we can set the text value at the 1,1 position as below,
1 2 3 4 5 6 |
StringGrid1->RowCount = 2; StringGrid1->ColCount = 2; StringGrid1->Cells[1][1] = U"This is Unicode String"; |
What are vectors in C++?
Vectors (std::vector) are dynamic arrays included in <vector> library in modern C++ and they can resize themselves automatically when a member of a vector is inserted or deleted. Vectors are the same as dynamic arrays and these dynamic arrays of vectors are handled automatically by the container. Vectors are the way of Modern C++, their members are placed in the contiguous memory storage, thus it can be resized, and it can be accessed and traversed using iterators.
When we Insert data into vectors, this may take a time than other static arrays because of the need of extending the vector array. Vectors have low memory usage as dynamic array implementations, because they have good data cache utilization and locality of reference. We can easily access an element of a vector by giving its index between ‘[‘ and ‘]’ as same as arrays, which means vector members can be referenced by indices.
Vectors allow random access; that is, an element of a vector may be referenced in the same manner as elements of arrays (by array indices). Linked lists and sets, on the other hand, do not support random access or pointer arithmetic. Vectors are very useful for storing data in lists whose number of elements (length in total) may not be known before setting up the list. Because the vector data structure allocates the necessary memory needed for specific data storage Erasing and clearing vector elements from a vector does not need to free any of the memory associated with that element. That makes vectors much more safe and modern in C++ than arrays.
Note that Arrays and structs in C++ are explained well in our other posts. You can use and add their values to StringGrids.
How do I define a vector in C++?
A vector can be defined in this syntax,
1 2 3 |
std::vector<object type> variable_name; |
Now let’s see how we can add vector member to a string.
- Create a new Windows VCL Application in C++ Builder and save all project files and unit into a directory.
- Drag a StringGrid (TStringGrid) and a Button (TButton) to your Form
- Double Click to Button and modify Button1Click inside as given on of the full examples below,
How do I list a one-dimensional vector in the columns of a StringGrid?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#include <vcl.h> #include <vector> #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { std::vector<int> vec{ 10, 20, 30, 40, 50, 60, 70, 80 , 90}; StringGrid1->ColCount = vec.size(); int i = 0; StringGrid1->BeginUpdate(); for (auto v: vec) { StringGrid1->Cells[i++][0] = IntToStr(v); } StringGrid1->EndUpdate(); } |
How can I list a one-dimensional vector in the rows of a StringGrid?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#include <vcl.h> #include <vector> #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { std::vector<int> vec{ 10, 20, 30, 40, 50, 60, 70, 80 , 90}; StringGrid1->RowCount = vec.size(); int j = 0; StringGrid1->BeginUpdate(); for (auto v: vec) { StringGrid1->Cells[0][j++] = IntToStr(v); } StringGrid1->EndUpdate(); } |
Is there an example of listing the contents of a two-dimensional vector in a StringGrid?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
#include <vcl.h> #include <vector> #pragma hdrstop #include "VectortoStringGrid_Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { int nCols =10, nRows= 5; // Defining a 2D Vector std::vector< std::vector <int>> vec; // Let's Add Some Values to 2D Vectors vec.resize(nRows); for(int i=0; i<nCols; i++) { vec[i].resize(nRows); for(int j=0; j<nRows; j++) { vec[i][j] = (j+1)*10+(i+1); } } //Setting Row and Column count of StringGrid StringGrid1->RowCount = nRows; StringGrid1->ColCount = nCols; // Copy 2D Vector Values to StringGrid StringGrid1->BeginUpdate(); for (int j=0; j<nRows; j++) { for (int i=0; i<nCols; i++) { StringGrid1->Cells[i][j] = IntToStr(vec[i][j]); } } StringGrid1->EndUpdate(); } |
Here is the application output,
Note that first rows and columns can be used as fixed row and column name holders or you can set FixedCols and FixedRows to zero to avoid them.
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition