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

Darryl Hoar 181 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.
4,103 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.
11,335 questions
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 72,841 Reputation points
    2023-03-10T21:08:27.6466667+00:00

    load the dateable from database.

    then add the column

    then loop thru the rows setting the column's calculated value.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,556 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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.