• Что бы вступить в ряды "Принятый кодер" Вам нужно:
    Написать 10 полезных сообщений или тем и Получить 10 симпатий.
    Для того кто не хочет терять время,может пожертвовать средства для поддержки сервеса, и вступить в ряды VIP на месяц, дополнительная информация в лс.

  • Пользаватели которые будут спамить, уходят в бан без предупреждения. Спам сообщения определяется администрацией и модератором.

  • Гость, Что бы Вы хотели увидеть на нашем Форуме? Изложить свои идеи и пожелания по улучшению форума Вы можете поделиться с нами здесь. ----> Перейдите сюда
  • Все пользователи не прошедшие проверку электронной почты будут заблокированы. Все вопросы с разблокировкой обращайтесь по адресу электронной почте : info@guardianelinks.com . Не пришло сообщение о проверке или о сбросе также сообщите нам.

Next Generation Data Grid for Delphi: Grouping

Sascha Оффлайн

Sascha

Заместитель Администратора
Команда форума
Администратор
Регистрация
9 Май 2015
Сообщения
1,486
Баллы
155
TMS Software Delphi  Components




Intro



If you’re developing in Delphi and looking for a powerful, flexible, and highly customizable data grid solution, then

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

is the perfect choice. In this blog, we continue on our journey with the TMS FNC Data Grid. The TMS FNC Data Grid offers developers advanced tools for presenting and interacting with data. Among these tools, grouping stand out as an essential feature, enabling users to quickly navigate and extract meaningful information from extensive data tables. In this blog post, we’ll explore how to implement grouping for one or multiple columns in the TMS FNC Data Grid, providing a step-by-step guide on how to optimize your grid for better data management and user experience.

Grouping

Grouping allows you to organize data within the TTMSFNCDataGrid based on one or more columns. This feature makes it easier to analyze datasets by visually categorizing and displaying related data together. The grid also supports various aggregation methods, such as calculating the sum, average, or count for grouped data, making it a powerful tool for data analysis.

Before diving into grouping, you'll need to configure your data grid. Here's an example setup:


Grid.Clear;

Grid.RowCount := 8;
Grid.ColumnCount := 3;

Grid.Cells[0, 0] := 'Department';
Grid.Cells[1, 0] := 'Employee Name';
Grid.Cells[2, 0] := 'Salary';

// IT Department
Grid.Cells[0, 1] := 'IT'; Grid.Cells[1, 1] := 'John Doe'; Grid.Cells[2, 1] := 50000;
Grid.Cells[0, 2] := 'IT'; Grid.Cells[1, 2] := 'Jane Smith'; Grid.Cells[2, 2] := 55000;
Grid.Cells[0, 3] := 'IT'; Grid.Cells[1, 3] := 'Mike Brown'; Grid.Cells[2, 3] := 60000;

// HR Department
Grid.Cells[0, 4] := 'HR'; Grid.Cells[1, 4] := 'Alice Brown'; Grid.Cells[2, 4] := 52000;
Grid.Cells[0, 5] := 'HR'; Grid.Cells[1, 5] := 'Bob Johnson'; Grid.Cells[2, 5] := 58000;

// Sales Department
Grid.Cells[0, 6] := 'Sales'; Grid.Cells[1, 6] := 'Mary White'; Grid.Cells[2, 6] := 62000;
Grid.Cells[0, 7] := 'Sales'; Grid.Cells[1, 7] := 'Tom Green'; Grid.Cells[2, 7] := 49000;

Enabling Grouping

To enable grouping, use the Group method to specify the columns that should be grouped. For instance, to group the rows by the Department column (column index 0), you can call the Group method:



Grid.Group(0); // Group by the first column

This will automatically group all rows by their values in the Department column. Now, all rows with the same department value will be grouped together. You will also be able to collapse and expand the grouped rows.

TMS Software Delphi  Components


Multi-Column Grouping


What if you want to group by more than one column? TTMSFNCDataGrid allows you to implement Multi-Column Grouping, where rows are grouped based on the values in multiple columns.


For example, let's say you want to group by both Department and Salary:


Grid.Group([0, 2]); // Group by the first and third columns


This will group the rows by Department first and then by Salary within each department group.

TMS Software Delphi  Components


Custom Grouping Logic



TTMSFNCDataGrid provides flexibility by allowing you to define custom grouping logic. This is useful when you want to group by derived values or need more complex conditions for grouping.

Using the OnGetCustomGroup event


To implement custom logic, you can use the OnGetCustomGroup event. Here's an example where we group employees based on whether their salary is above or below 55,000, rather than by the salary value itself:


procedure TForm1.GridGetCustomGroup(Sender: TObject; ACell: TTMSFNCDataGridCellCoord;
AData: TTMSFNCDataGridCellValue;
ALevel: Integer; var AGroup: string);
begin
if ACell.Column = 2 then
begin
if AData.AsInteger > 55000 then
AGroup := 'Above 55K'
else
AGroup := 'Below 55K';
end;
end;



In this case, instead of grouping by the exact salary values, the data will be grouped into "Above 55K" and "Below 55K" categories. You can then call the Group method like this:


Grid.Group(2);

TMS Software Delphi  Components


Advanced Grouping Options

TTMSFNCDataGrid offers several advanced grouping options, such as the ability to summarize groups, merge headers, and auto-sort groups.

[FONT=Calibri, sans-serif]Here’s a breakdown of some of the most useful group-related options:[/FONT]
  • [FONT=Calibri, sans-serif]Summary: Add a summary row at the end of each group.[/FONT]
  • [FONT=Calibri, sans-serif]MergeHeader: Merge grouped column headers for a cleaner look.[/FONT]
  • [FONT=Calibri, sans-serif]AutoSort: Automatically sort rows when a group is created.[/FONT]

You can customize these options by using the `OnGetGroupOptions` event:


procedure TForm1.GridGetGroupOptions(Sender: TObject; AColumns: TArray<Integer>;
ALevel: Integer; var AOptions: TTMSFNCDataGridDataGroupOptions);
begin
AOptions.Summary := True;
AOptions.MergeHeader := True;
AOptions.AutoSort := True;
end;

These settings will automatically apply summaries to each group, merge the headers for grouped columns, and ensure that the grouped data is sorted.

Conclusion


Grouping in TTMSFNCDataGrid is a powerful feature that enhances data organization and allows for more complex data manipulation. Whether you're grouping by a single column, multiple columns, or using custom logic, TTMSFNCDataGrid offers a flexible and robust set of options.

By utilizing events such as `OnGetCustomGroup` and `OnGetGroupOptions`, you can create custom groupings and apply advanced settings to your data grid, allowing for better data presentation and user interaction.

With grouping, you enable users to explore and analyze data efficiently. So, whether you're working on a business application with financial data or an inventory management system, TTMSFNCDataGrid&#146;s grouping functionality can significantly improve data usability and insights.

In the next blog we'll explore how you can add real controls to cells, so stay tuned for more advanced tips and tutorials on leveraging this next-generation data grid for your Delphi projects! Happy coding!


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

 
Вверх Снизу