How to sum columns in a dataTable? - c#

How can I get a sum for all the columns in a datatable? Say I had the following table. How can I calculate the "total" row? It should be easy to add total row to a datatable.
Columns hits uniques sigups, etc...
Rows
1 12 1 23
2 1 0 5
3 6 2 9
total 19 3 37
Update
I ended up with this. It was the only thing I could get to work.
For Each col As DataColumn In TotalsTable.Columns
If col.DataType.Name = "DateTime" Then
count = count + 1
Continue For
End If
Dim colTotal As Double = 0
Dim value As Double
For Each row As DataRow In TotalsTable.Rows
If Double.TryParse(row(col), value) Then
colTotal += Double.Parse(row(col))
End If
Next
totalRow(count) = colTotal
count = count + 1
Next

There is also a way to do this without loops using the DataTable.Compute Method. The following example comes from that page. You can see that the code used is pretty simple.:
private void ComputeBySalesSalesID(DataSet dataSet)
{
// Presumes a DataTable named "Orders" that has a column named "Total."
DataTable table;
table = dataSet.Tables["Orders"];
// Declare an object variable.
object sumObject;
sumObject = table.Compute("Sum(Total)", "EmpID = 5");
}
I must add that if you do not need to filter the results, you can always pass an empty string:
sumObject = table.Compute("Sum(Total)", "")

Try this:
DataTable dt = new DataTable();
int sum = 0;
foreach (DataRow dr in dt.Rows)
{
foreach (DataColumn dc in dt.Columns)
{
sum += (int)dr[dc];
}
}

I doubt that this is what you want but your question is a little bit vague
Dim totalCount As Int32 = DataTable1.Columns.Count * DataTable1.Rows.Count
If all your columns are numeric-columns you might want this:
You could use DataTable.Compute to Sum all values in the column.
Dim totalCount As Double
For Each col As DataColumn In DataTable1.Columns
totalCount += Double.Parse(DataTable1.Compute(String.Format("SUM({0})", col.ColumnName), Nothing).ToString)
Next
After you've edited your question and added more informations, this should work:
Dim totalRow = DataTable1.NewRow
For Each col As DataColumn In DataTable1.Columns
totalRow(col.ColumnName) = Double.Parse(DataTable1.Compute("SUM(" & col.ColumnName & ")", Nothing).ToString)
Next
DataTable1.Rows.Add(totalRow)

You can loop through the DataColumn and DataRow collections in your DataTable:
// Sum rows.
foreach (DataRow row in dt.Rows) {
int rowTotal = 0;
foreach (DataColumn col in row.Table.Columns) {
Console.WriteLine(row[col]);
rowTotal += Int32.Parse(row[col].ToString());
}
Console.WriteLine("row total: {0}", rowTotal);
}
// Sum columns.
foreach (DataColumn col in dt.Columns) {
int colTotal = 0;
foreach (DataRow row in col.Table.Rows) {
Console.WriteLine(row[col]);
colTotal += Int32.Parse(row[col].ToString());
}
Console.WriteLine("column total: {0}", colTotal);
}
Beware: The code above does not do any sort of checking before casting an object to an int.
EDIT: add a DataRow displaying the column sums
Try this to create a new row to display your column sums:
DataRow totalsRow = dt.NewRow();
foreach (DataColumn col in dt.Columns) {
int colTotal = 0;
foreach (DataRow row in col.Table.Rows) {
colTotal += Int32.Parse(row[col].ToString());
}
totalsRow[col.ColumnName] = colTotal;
}
dt.Rows.Add(totalsRow);
This approach is fine if the data type of any of your DataTable's DataRows are non-numeric or if you want to inspect the value of each cell as you sum. Otherwise I believe #Tim's response using DataTable.Compute is a better.

It's a pity to use .NET and not use collections and lambda to save your time and code lines
This is an example of how this works:
Transform yourDataTable to Enumerable, filter it if you want , according a "FILTER_ROWS_FIELD" column, and if you want, group your data by a "A_GROUP_BY_FIELD".
Then get the count, the sum, or whatever you wish.
If you want a count and a sum without grouby don't group the data
var groupedData = from b in yourDataTable.AsEnumerable().Where(r=>r.Field<int>("FILTER_ROWS_FIELD").Equals(9999))
group b by b.Field<string>("A_GROUP_BY_FIELD") into g
select new
{
tag = g.Key,
count = g.Count(),
sum = g.Sum(c => c.Field<double>("rvMoney"))
};

for (int i=0;i<=dtB.Columns.Count-1;i++)
{
array(0, i) = dtB.Compute("SUM([" & dtB.Columns(i).ColumnName & "])", "")
}

Related

Manipulating Column in DataTable C#

Am using DataTable in C# and am trying to manipulate, modify one of the column. Consider sample data below
Id City Temperature
-------------------
1 A -12
2 B 23
3 C 12
And after conversion i want the below result where am converting Minus to M and Positive values to P
Id City Temperature
-------------------------
1 A 12M
2 B 23P
3 C 12P
Can i achieve this using LINQ..Am parsing this with around 50k Rows and dont want to compromise on performance.what are the other best ways ?
If the column is string instead of double/int:
foreach(DataRow row in table.Rows)
{
string temp = row.Field<string>("Temperature");
bool negative = temp.StartsWith("-");
temp = negative ? temp.Substring(1) + "M" : temp + "P";
row.SetField("Temperature", temp);
}
If the column type is double - as mentioned now - you have to create a new DataTable. You cannot change the DataType after the Datatable is filled with data.
DataTable newTable = table.Clone();
int ordinal = newTable.Columns.IndexOf("Temperature");;
newTable.Columns.Remove("Temperature"); // remove double-column
DataColumn tempCol = newTable.Columns.Add("Temperature"); // string
tempCol.SetOrdinal(ordinal);
foreach (DataRow row in table.Rows)
{
DataRow newRow = newTable.Rows.Add();
foreach(DataColumn col in newTable.Columns)
{
if (col == tempCol)
{
double temp = row.Field<double>("Temperature");
bool negative = temp < 0;
double abs = Math.Abs(temp);
string newTemp = negative ? abs.ToString() + "M" : abs.ToString() + "P";
newRow.SetField(col, newTemp);
}
else
newRow.SetField(col, row[col.ColumnName]);
}
}

how to dynamically get a single value from a datatable in C#?

I am new at C#, I have a DataTable named dt; Now I want to get the values from it's each row and a specific column named "Number" from which I can calculate a third column to add. But cant' do it. Any ideas? Please help me.
foreach (DataRow dRow in dt.Rows)
{
int number = dt.Rows[0].Field<int>(1);
dRow[Ratio] = Convert.ToString(((number * 100) / grandTotal)) + " %";
}
Use a loop and the Field method, presuming that Number is an int:
foreach(DataRow row in dt.Rows)
{
int number = row.Field<int>("Number");
// do your calculation
row.SetField("ThirdColumn", someValue);
}

Converting a List into Datatable

Assume that I have a list like PL = { P1, 0, 10, P2, 5, 20 } and I need to convert it a datatable like
ProcessName ArrivalTime CpuTime
P1 0 10
P2 5 20
The number of process (row count) is dynamic. And I have tried sth like this:
protected DataTable CreateDataTable()
{
int j = 0;
List<string> PL = CreateProcessList();
DataTable DT = new DataTable();
for (int i = 0; i < PL.Count - 2; i += 3)
{
DataRow ProcessRow = DT.NewRow();
DT.Rows[j][0] = PL[i].ToString();
DT.Rows[j][1] = Convert.ToInt32(PL[i + 1]);
DT.Rows[j][2] = Convert.ToInt32(PL[i + 2]);
j++;
}
DT.Columns.Add("Header", typeof(string));
DT.Columns[0].ColumnName = "ProcessName";
DT.Columns[1].ColumnName = "ArrivalTime";
DT.Columns[2].ColumnName = "CpuTime";
return DT;
}
It does not work (says that there is no row at position 0). Thanks for any idea.
Working Code After Editions:
protected DataTable CreateDataTable()
{
List<string> PL = CreateProcessList();
DataTable DT = new DataTable();
DT.Columns.Add("ProcessName", typeof(string));
DT.Columns.Add("ArrivalTime", typeof(int));
DT.Columns.Add("CpuTime", typeof(int));
for (int i = 0; i < PL.Count - 2; i += 3)
{
DataRow ProcessRow = DT.NewRow();
ProcessRow[0] = PL[i].ToString();
ProcessRow[1] = Convert.ToInt32(PL[i + 1]);
ProcessRow[2] = Convert.ToInt32(PL[i + 2]);
DT.Rows.Add(ProcessRow);
}
return DT;
}
To create a datatable in the way you have described, you need to follow a different way.
Create a datatable object
Add Columns to the datatable object by using the Add() method
use the datatable objects NewRow() method to get a DataRow object with the same schema as your datatable
populate the columns of this DataRow with the desired values
Add this DataRow to the Rows collection of your datatable object by using the Add() method
repeat step 3 to 6 until your list reaches the end.
On the second iteration of your for loop i is 3, so you're getting the 4th row of your table (at this point in time your table has 2 row). You're then getting the 4th, 5th, and 6th columns of that row (your table has 0 columns, as you haven't added any columns yet) to set their value. The corresponding index out of range errors should be telling you exactly what's wrong here.
Don't access the i-th row from the table. Just use ProcessRow to access the row; it's right there in a variable for you. Don't access the i-th column, access the 1st, 2nd, and 3rd (and add the columns before you try to populate them.
Something like this should work. Keep adding to curRow until the current iteration starts with 'P'. When it does start with 'P', add the currentRow to the data table and start a new row.
DataTable dataTable;
DataRow curRow;
... add columns to dataTable
for (var i = 0; i < PL.Count; i++) {
if (PL[i].ToString().StartsWith("P")) {
if (curRow != null)
dataTable.Rows.Add(curRow);
curRow = dataTable.NewRow();
}
... add PL[i] to curRow
}
There's a few little issues with this but they can be fixed pretty easily. A check to make sure curRow is not null before adding to data table... that sorta thing.

Updating values in DataTable doesn't work

I have code like this:
DataTable dt = new DataTable();
// (...) getting data, displaying on DataGridView - all works fine
int columns = dt.Columns.Count; // getting column count
foreach (DataRow row in dt.Rows)
{
for (int c = 0; c < columns; c++) // c is column index
{
// all old values are positive for debugging
double oldVal = Convert.ToDouble(row.ItemArray[c]);
// new values should become negative
double newVal = oldVal * -1;
row.ItemArray[c] = newVal; // im trying to update value like this
// THIS SHOWS POSITIVE NUMBERS (NOT UPDATED)
this.Text = row.ItemArray[c].ToString(); // this is simple debug
}
}
This is a little more complicated, i simplified code.
Why my values are not updated?
Added later:
One more important thing. This data comes from database view, not table. Of course I want to change that data in my DataTable object, not in database.
In the end do this
dt.AcceptChanges();
This Commits all the changes made to this table since the last time AcceptChanges() was called.
DataTable dt = new DataTable();
// (...) getting data, displaying on DataGridView - all works fine
int columns = dt.Columns.Count; // getting column count
foreach (DataRow row in dt.Rows)
{
foreach (DataColumn c in dt.Columns)
{
// all old values are positive for debugging
double oldVal = Convert.ToDouble(row[c]);
// new values should become negative
double newVal = oldVal * -1;
row[c] = newVal; // im trying to update value like this
// THIS SHOWS POSITIVE NUMBERS (NOT UPDATED)
this.Text = row[c].ToString(); // this is simple debug
}
}
dt.AcceptChanges();
EDIT (Added explaination):
Changes to ItemArray elements are not tracked, so no changes are reflected in the datatable values
However you can use ItemArray to change all the row at once, like this:
dt.Rows[0].ItemArray = new object[] {"new value"};
In this case the changes are tracked, and are reflected in datatable.
Update your foreach loop as
foreach (DataRow row in dt.Rows)
{
for (int c = 0; c < columns; c++) // c is column index
{
double oldVal = Convert.ToDouble(row[c]);
double newVal = -oldVal;
row[c] = newVal;
this.Text = row[c].ToString();
}
}
or you can use foreachinstead of for loop as:
foreach (DataRow row in dt.Rows)
{
foreach (DataColumn c in dt.Columns)
{
double oldVal = Convert.ToDouble(row[c]);
double newVal = -oldVal;
row[c] = newVal;
this.Text = row[c].ToString();
}
}

Find a string in all DataTable columns

I am trying to find a fast way to find a string in all datatable columns!
Followed is not working as I want to search within all columns value.
string str = "%whatever%";
foreach (DataRow row in dataTable.Rows)
foreach (DataColumn col in row.ItemArray)
if (row[col].ToString() == str) return true;
You can use LINQ. It wouldn't be any faster, because you still need to look at each cell in case the value is not there, but it will fit in a single line:
return dataTable
.Rows
.Cast<DataRow>()
.Any(r => r.ItemArray.Any(c => c.ToString().Contains("whatever")));
For searching for random text and returning an array of rows with at least one cell that has a case-insensitive match, use this:
var text = "whatever";
return dataTable
.Rows
.Cast<DataRow>()
.Where(r => r.ItemArray.Any(
c => c.ToString().IndexOf(text, StringComparison.OrdinalIgnoreCase) > 0
)).ToArray();
If you want to check every row of every column in your Datatable, try this (it works for me!).
DataTable YourTable = new DataTable();
// Fill your DataTable here with whatever you've got.
foreach (DataRow row in YourTable.Rows)
{
foreach (object item in row.ItemArray)
{
//Do what ya gotta do with that information here!
}
}
Don't forget to typecast object item to whatever you need (string, int etc).
I've stepped through with the debugger and it works a charm. I hope this helps, and good luck!
This can be achieved by filtering. Create a (re-usable) filtering string based on all the columns:
bool UseContains = false;
int colCount = MyDataTable.Columns.Count;
string likeStatement = (UseContains) ? " Like '%{0}%'" : " Like '{0}%'";
for (int i = 0; i < colCount; i++)
{
string colName = MyDataTable.Columns[i].ColumnName;
query.Append(string.Concat("Convert(", colName, ", 'System.String')", likeStatement));
if (i != colCount - 1)
query.Append(" OR ");
}
filterString = query.ToString();
Now you can get the rows where one of the columns matches your searchstring:
string currFilter = string.Format(filterString, searchText);
DataRow[] tmpRows = MyDataTable.Select(currFilter, somethingToOrderBy);
You can create a routine of search with an array of strings with the names of the columns, as well:
string[] elems = {"GUID", "CODE", "NAME", "DESCRIPTION"};//Names of the columns
foreach(string column in elems)
{
string expression = string.Format("{0} like '%{1}%'",column,
txtSearch.Text.Trim());//Search Expression
DataRow[] row = data.Select(expression);
if(row.Length > 0) {
// Some code here
} else {
// Other code here
}
}
You can get names of columns by using ColmunName Method. Then, you can search every column in DataTable by using them. For example, follwing code will work.
string str = "whatever";
foreach (DataRow row in dataTable.Rows)
{
foreach (DataColumn column in dataTable.Columns)
{
if (row[column.ColumnName.ToString()].ToString().Contains(str))
{
return true;
}
}
}
You can create a filter expression on the datatable as well. See this MSDN article. Use like in your filter expression.
string filterExp = "Status = 'Active'";
string sortExp = "City";
DataRow[] drarray;
drarray = dataSet1.Customers.Select(filterExp, sortExp, DataViewRowState.CurrentRows);
for (int i=0; i < drarray.Length; i++)
{
listBox1.Items.Add(drarray[i]["City"].ToString());
}

Categories