How to add a calculated column where the calculation is complex in c#

Darryl Hoar 141 Reputation points
2023-03-10T20:58:57.0633333+00:00

I am using visual studio 2019. I have created a project. The project has winform. On the form, it has a datagridview component.

I am retrieving data from an sql database table and filling a datatable with it. The datatable is the datasource for the datagridview.

I want to add a column to the datagridview which contains calculated values. The sql table has a decimal field call occupancy. It is a decimal field. This decimal field needs to be processed to create a string of the format "00:00:00". I have a function created that takes the decimal value and returns the processes string, but I am unable to figure out how to :

dataTable1.Columns.Add("calc_occupancy",typeof(NVarChar),"whatevershouldgohere");

Any ideas greatly appreciated.

Thanks.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,367 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,233 questions
{count} votes

1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,036 Reputation points
    2023-03-10T23:36:06.9333333+00:00

    Consider a computed column. Its best done in SSMS then query the table as you would any table.