Blog

All Blog Posts  |  Next Post  |  Previous Post

Next Generation Data Grid for Delphi: Adding, Formatting & Converting Data

Tuesday, October 1, 2024

Intro

If you’re developing in Delphi and looking for a powerful, flexible, and highly customizable data grid solution, then TMS FNC Data Grid is the perfect choice. In this blog, we continue on our journey with the TMS FNC Data Grid. When working with data grids, effectively managing data is essential for creating user-friendly interfaces. Whether you're building dashboards, data entry forms, or reporting tools, TTMSFNCDataGrid makes it simple to add, format, and convert various types of data. In this post, we'll explore how to add data to your grid, format it for better presentation, and convert it between different types — all based on the TValue type that powers the grid’s Cells property.

What is TMS FNC Data Grid?

To have a better understanding on what TMS FNC Data Grid is and has to offer, please read through this blog first.

Understanding TValue and the Cells Property

At the heart of TTMSFNCDataGrid's data handling is the TValue type. This flexible type allows you to store different kinds of data (e.g., strings, integers, floating-point numbers, booleans, and TDateTime) in the grid cells. The grid's Cells property utilizes TValue to hold this variety of data seamlessly, making it extremely versatile for any data structure.

Here’s a basic example of how to add different types of data to the grid:

Grid.Cells[0, 1] := 1;            // Integer
Grid.Cells[1, 1] := 'John Doe';   // String
Grid.Cells[2, 1] := 'USA';        // String

In this example, we added an integer to the first column, a string in the second column, and another string in the third column. The Cells property automatically interprets and stores these different data types using TValue.

Since TValue supports multiple data types, you can also store floating-point numbers and date/time values:

Grid.Cells[0, 1] := 1234.56;     // Floating-point number
Grid.Cells[1, 1] := Now;         // Current date and time (TDateTime)
Grid.Cells[2, 1] := 'USA';       // String
By using TValue, TTMSFNCDataGrid provides great flexibility in handling various data types without needing explicit type conversions, simplifying data management in your grid.

Example:

// Set the number of rows and columns
Grid.RowCount := 2;  // 2 rows for more data
Grid.ColumnCount := 5;  // 5 columns for different data types

// Add column headers
Grid.Cells[0, 0] := 'Name';
Grid.Cells[1, 0] := 'Join Date';
Grid.Cells[2, 0] := 'Status';
Grid.Cells[3, 0] := 'Years of Experience';
Grid.Cells[4, 0] := 'Salary';

Grid.Cells[0, 1] := 'Alice Johnson';  // Name (string)
Grid.Cells[1, 1] := EncodeDate(2022, 5, 22);  // Join Date (TDateTime)
Grid.Cells[2, 1] := True;  // Status (Boolean)
Grid.Cells[3, 1] := 5;  // Years of Experience (Integer)
Grid.Cells[4, 1] := 75000.50;  // Salary (Float)

Grid.AutoSizeGrid();
In this example, we’ve added a name, join date, status (boolean), years of experience (integer), and salary (float). These values can be easily modified or retrieved later thanks to the flexibility of TValue.

TMS Software Delphi  Components

You'll notice that the rendering is based on numerical values, as TDateTime is represented as a Double, with the default formatting displaying it as a decimal number. To change this formatting, you can either use the Formatting property at the column level or handle it through the OnGetCellFormatting event.

Formatting Data in the Grid

Once your data is in the grid, you might want to present it in a more readable or user-friendly way. TTMSFNCDataGrid provides powerful formatting options, especially at the column level. This ensures that all cells within a column follow a consistent format.

You can format entire columns by setting the Formatting property. For example, if you’re working with date values, you can format them to display in a specific format, such as yyyy/mm/dd:

Grid.Columns[1].Formatting.&Type := gdftDate;    // Set type to Date
Grid.Columns[1].Formatting.Format := 'yyyy/mm/dd';  // Display date in yyyy/mm/dd format
Grid.Columns[1].AddSetting(gcsFormatting);

By applying formatting at the column level, you ensure that all data in that column is displayed consistently, making your grid data easier to read.

TMS Software Delphi  Components

Handling Custom Formatting with Events

In addition to column-level formatting, you can customize the formatting of individual cells by handling the OnGetCellFormatting event. This event allows you to define specific formatting for different types of data as they are rendered in the grid.

For example, let’s say we want to format dates, booleans, and numbers differently:

  1. Join Date: Display as yyyy/mm/dd.
  2. Status: Show Approved for True and Pending for False.
  3. Years of Experience: Display as integers.
  4. Salary: Format as currency with a dollar sign and two decimal places.

Here’s how you can handle this in the OnGetCellFormatting event:

procedure TForm1.GridGetCellFormatting(Sender: TObject; ACell: TTMSFNCDataGridCellCoord;
  AData: TTMSFNCDataGridCellValue; var AFormatting: TTMSFNCDataGridDataFormatting;
  var AConvertSettings: TFormatSettings; var ACanFormat: Boolean);
begin
  // Skip formatting for the first row (header)
  if ACell.Row = 0 then Exit;

  ACanFormat := True;

  // Column 1: Format as date
  if ACell.Column = 1 then
  begin
    AFormatting.Format := 'yyyy/mm/dd';  // Custom date format
    AFormatting.&Type := gdftDate;       // Set type to Date
  end;

  // Column 2: Format as boolean with custom text
  if ACell.Column = 2 then
  begin
    AFormatting.&Type := gdftBoolean;
    AFormatting.BooleanTrueText := 'Approved';
    AFormatting.BooleanFalseText := 'Pending';
  end;

  // Column 3: Format as integer
  if ACell.Column = 3 then
  begin
    AFormatting.Format := '%g';  // Format integers
    AFormatting.&Type := gdftNumber;  // Set type to Number
  end;

  // Column 4: Format salary as currency
  if ACell.Column = 4 then
  begin
    AFormatting.Format := '$#,##0.00';  // Currency format
    AFormatting.&Type := gdftFloat;    // Set type to Float
  end;
end;
procedure TForm1.PopulateGrid;
begin
  // Set the number of rows and columns
  Grid.RowCount := 5;  // 5 rows for more data
  Grid.ColumnCount := 5;  // 5 columns for different data types

  // Add column headers
  Grid.Cells[0, 0] := 'Name';
  Grid.Cells[1, 0] := 'Join Date';
  Grid.Cells[2, 0] := 'Status';
  Grid.Cells[3, 0] := 'Years of Experience';
  Grid.Cells[4, 0] := 'Salary';

  // Add sample data to the grid
  Grid.Cells[0, 1] := 'Jane Smith';
  Grid.Cells[1, 1] := EncodeDate(2022, 5, 22);  // Example join date
  Grid.Cells[2, 1] := False;  // Pending
  Grid.Ints[3, 1] := 5;  // Years of experience
  Grid.Floats[4, 1] := 75000.50;  // Salary with decimals

  Grid.Cells[0, 2] := 'John Doe';
  Grid.Cells[1, 2] := EncodeDate(2019, 7, 10);  // Example join date
  Grid.Cells[2, 2] := True;  // Approved
  Grid.Ints[3, 2] := 8;  // Years of experience
  Grid.Floats[4, 2] := 85000.75;  // Salary with decimals

  Grid.Cells[0, 3] := 'Alice Brown';
  Grid.Cells[1, 3] := EncodeDate(2021, 11, 3);  // Example join date
  Grid.Cells[2, 3] := False;  // Pending
  Grid.Ints[3, 3] := 3;  // Years of experience
  Grid.Floats[4, 3] := 62000.00;  // Salary with no decimals

  Grid.Cells[0, 4] := 'Bob Johnson';
  Grid.Cells[1, 4] := EncodeDate(2018, 9, 15);  // Example join date
  Grid.Cells[2, 4] := True;  // Approved
  Grid.Ints[3, 4] := 12;  // Years of experience
  Grid.Floats[4, 4] := 95000.00;  // Salary with no decimals

  Grid.AutoSizeGrid();
end;
TMS Software Delphi  Components

Converting Data

Since TTMSFNCDataGrid stores its cell data using the versatile TValue type, it’s easy to convert between different data types when needed. You can use built-in functions to convert TValue to specific types, such as integers, floats, booleans, and strings:

  • Convert to Integer: TValue.ToInteger
  • Convert to String: TValue.ToString
  • Convert to Float: TValue.ToFloat
  • Convert to Boolean: TValue.ToBoolean

Example:

var
  Value: TValue;
  IntValue: Integer;
  StrValue: String;
begin
  Value := Grid.Cells[1, 1];   // Fetch cell data as TValue

  if Value.IsInteger then
    IntValue := Value.AsInteger;

  StrValue := Value.AsString;  // Convert to string
end;

This flexibility allows you to retrieve and manipulate data in the format you need for calculations, validations, or display purposes.

Converting String Data to Dates

Another frequent need is to convert string representations of dates into TDateTime values. This is common when loading data from external sources, where dates are often stored as strings in various formats.

Example: Converting String Data to TDateTime Using ConvertSettings

If your grid contains date values stored as strings (e.g., "31-12-2023"), you can use the ConvertSettings to transform these strings into TDateTime values that the grid will display properly:

procedure TForm1.GridGetCellFormatting(Sender: TObject; ACell: TTMSFNCDataGridCellCoord;
  AData: TTMSFNCDataGridCellValue; var AFormatting: TTMSFNCDataGridDataFormatting;
  var AConvertSettings: TFormatSettings; var ACanFormat: Boolean);
begin
  // Column 1 is expected to contain date data in string format
  if ACell.Column = 1 then
  begin
    AConvertSettings.ShortDateFormat := 'dd-mm-yyyy';  // Define the input date format
    AConvertSettings.DateSeparator := '-';             // Specify the date separator

    AFormatting.Format := 'yyyy/mm/dd';  // Format the output as yyyy/mm/dd
    AFormatting.&Type := gdftDate;       // Indicate the data should be treated as a date

    ACanFormat := True;  // Allow the formatting to proceed
  end;
end;

Here, the ConvertSettings define how to interpret the input string ("31-12-2023") and convert it into a TDateTime value. The grid will then display the date in the yyyy/mm/dd format, even though the input was in a different string format.

Conclusion

The TMS FNC Data Grid is a powerful and flexible component for Delphi developers, offering extensive features for displaying, managing, and interacting with data. Whether you're building a desktop, mobile or web application, this grid can handle a wide range of data scenarios while providing a sleek, modern user interface.

In this blog, we’ve covered how to add, format and convert your data, but its capabilities go far beyond what we’ve shown here. We encourage you to explore its features to fully unlock its potential in your applications.

In the next blog we'll dive deeper into filtering & sorting, so stay tuned for more advanced tips and tutorials on leveraging this next-generation data grid for your Delphi projects! Happy coding!



Pieter Scheldeman


  1. Next Generation Data Grid for Delphi: Getting Started

  2. Next Generation Data Grid for Delphi: Adding, Formatting & Converting Data

  3. Next Generation Data Grid for Delphi: Filtering & Sorting

  4. It's here: Next Generation Data Grid for Delphi 1.0

  5. Next Generation Data Grid for Delphi: Webinar Replay Available!



This blog post has not received any comments yet.



Add a new comment

You will receive a confirmation mail with a link to validate your comment, please use a valid email address.
All fields are required.



All Blog Posts  |  Next Post  |  Previous Post