Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to calculate datatable size in C#
Posted

for counting rows
C#
datatable_Name.Rows.Count

and for columns
C#
datatable_Name.Columns.Count
 
Share this answer
 
v2
try this code for that

using System;
using System.Data;

public class DataTableComputeExample
{
    public static void Main()
    {
        //adding up a new datatable
        DataTable dtEmployee = new DataTable("Employee"); 

        //adding up 3 columns to datatable
        dtEmployee.Columns.Add("ID", typeof(int));
        dtEmployee.Columns.Add("Name", typeof(string));
        dtEmployee.Columns.Add("Salary", typeof(double));

        //adding up rows to the datatable
        dtEmployee.Rows.Add(52, "Human1", 21000);
        dtEmployee.Rows.Add(63, "Human2", 22000);
        dtEmployee.Rows.Add(72, "Human3", 23000);
        dtEmployee.Rows.Add(110,"Human4", 24000);

        Console.Write("Employee Count:");
        Console.WriteLine(dtEmployee.Compute("Count(ID)", string.Empty));

        Console.Write("Maximum Salary:");
        Console.WriteLine(dtEmployee.Compute("Max(Salary)",string.Empty));

        Console.Write("Minimum Salary:");
        Console.WriteLine(dtEmployee.Compute("Min(Salary)", string.Empty));

        Console.Write("Average Salary:");
        Console.WriteLine(dtEmployee.Compute("Avg(Salary)", string.Empty));

        Console.Write("Sum Salary:");
        Console.WriteLine(dtEmployee.Compute("Sum(Salary)", string.Empty));

        Console.ReadLine();
    }

}

Output:

Employee Count:4
Maximum Salary:24000
Minimum Salary:21000
Average Salary:22500
Sum Salary:90000
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900