C++ Builder has a lot of great components you can use to create a C++ app and StringGrid (TStringGrid) is one of these. A StringGrid represents a grid control designed to simplify the handling of strings.
We can easily add a TStringGrid object to a form to represent textual data in a tabular format. StringGrid 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. StringGrid 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.
What are the properties of TStringGrid?
ColCount and RowCount
ColCount specifies the number of columns in the grid. We can read Column Count to determine the number of entries in the Columns array or we can add or delete rows at the bottom of the grid.
RowCount specifies the number of rows in the grid, The value of RowCount includes the scrollable rows in the grid, but not the fixed row with the headers. We can read RowCount to determine the number of rows in the grid or we can set RowCount to add or delete rows at the bottom of the grid.
Here is a example below,
1 2 3 4 |
int col = StringGrid1->ColumnCount; int row = StringGrid1->RowCount; |
Cells Array
Cells array lists the strings for each cell in the grid. We can use Cells to access the string within a particular cell. ACol
is the column coordinate of the cell, and ARow
is the row coordinate of the cell. The first row is row zero, and the first column is column zero. The ColCount and RowCount property values define the size of the array of strings. Each grid member is String member which means they are UnicodeString, can be used with worldwide languages.
1 2 3 4 5 6 7 8 9 10 |
StringGrid1->ColumnCount = 2; StringGrid1->RowCount = 2; StringGrid1->Cells[0][0]= L"0-0"; StringGrid1->Cells[0][1]= L"0-1"; StringGrid1->Cells[1][0]= L"1-0"; StringGrid1->Cells[1][1]= L"1-1"; |
Col and Row
Col property is the column number of the selected cell, or -1
if there is no cell selected.
Row is the number of the selected row.
1 2 3 4 |
int col = StringGrid1->ColumnCount; int row = StringGrid1->RowCount; |
DefaultColWidth and DefaultRowHeight
A StringGrid Example on a VCL form
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 |
#include <vcl.h> #include <clipbrd.hpp> #pragma hdrstop #include "Excel_to_StringGrid_VCL_Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void split(wchar_t delimiter, UnicodeString str, TStringList *listofstrings) { listofstrings->Clear(); listofstrings->Delimiter = delimiter; listofstrings->StrictDelimiter = True; // Requires D2006 or newer. listofstrings->DelimitedText = str; } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { UnicodeString us; TStringList *str_rows= new TStringList(); TStringList *str_cols= new TStringList(); split( L'\n', Clipboard()->AsText, str_rows); //Delimit lines with return char StringGrid1->RowCount=str_rows->Count; for(int j=0; j<str_rows->Count; j++) { if(str_cols->Count>StringGrid1->ColCount) StringGrid1->ColCount=str_cols->Count; split( L'\t', str_rows->Strings[j], str_cols); //Delimit line with tab char for(int i=0; i<str_cols->Count; i++) { Memo1->Lines->Add(IntToStr(i)+","+IntToStr(j)+"="+str_cols->Strings[i]); StringGrid1->Cells[i][j]=str_cols->Strings[i]; } } } |
You can download a free trial of C++ Builder right now.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition