C++ComponentsLearn C++

This Is How To List Vector Values In A C++ StringGrid

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.

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 :

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:

For example we can define a 2×2 StringGrid table and we can set the text value at the 1,1 position as below,

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,

Now let’s see how we can add vector member to a string.

  1. Create a new Windows VCL Application in C++ Builder and save all project files and unit into a directory.
  2. Drag a StringGrid (TStringGrid) and a Button (TButton) to your Form
  3. 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?

How can I list a one-dimensional vector in the rows of a StringGrid?

Is there an example of listing the contents of a two-dimensional vector in a StringGrid?

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.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?