how I can show the sum of in a datagridview column?
int sum = 0;
for (int i = 0; i < dataGridView1.Rows.Count; ++i)
{
sum += Convert.ToInt32(dataGridView1.Rows[i].Cells[1].Value);
}
label1.Text = sum.ToString();
Calculate sum of a column in a datagridview c#
In your method, you call the row "row" inside the loop, but declare it as r in the foreach.
I would recommend simplifying your code though. Have a look at the answer here: how I can show the sum of in a datagridview column?
I don't think you need the a & b variables to get at your answer and perhaps you are initializing them differently in the 1st example when you are passing them into the method.
int a= 0;
foreach (DataGridViewRow r in dataGridView1.Rows){
{
a += Convert.ToInt32(r.Cells[c].Value);
}
DatagridView C# Sum of columns
You could use this query:
var userSums = dataGridView2.Rows.Cast<DataGridViewRow>()
.GroupBy(row => row.Cells[0].Value.ToString())
.Select(g => new { User = g.Key, Sum = g.Sum(row => Convert.ToInt32(row.Cells[1].Value)) });
How to get the sum of datagridview columns in C#
It looks like you are not using the correct values for your column names.
For example, this code:
row.Cells[dataGridView1.Columns["Total Test"].Index].Value
Is not going to return a column, since the string "Total Test" is neither a column index or a column name (a column name cannot have spaces). It looks like you are trying to use the column header text. Change it to:
row.Cells[0].Value
(where 0 is the index of the column)
Or:
row.Cells["colTotalTest"].Value
(where colTotalTest is the name of the column)
And do the same for all references to columns.
how I can show the sum of in a datagridview column in asp.net
use table.Compute function
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");
//Find label
GridViewRow footer = dgOpenBal.FooterRow;
var lblTotal = (Label)footer.FindControl("lblTotal");
lblTotal.Text = sumObject.ToString();
}
Another approach use stored procedure to get calculated amount from Sproc
private void ComputeBySalesSalesID(DataSet ds)
{
if (ds.Tables[0].Rows.Count > 0)
{
DataRow drSum = ds.Tables[0].Rows[0];
GridViewRow footer = dgOpenBal.FooterRow;
var lblTotal = (Label)footer.FindControl("lblTotal");
lblTotal.Text = drSum["sum"].ToString();
}
}
Related Topics
Adding Unknown (At Design Time) Properties to an Expandoobject
Fluent API, Many-To-Many in Entity Framework Core
Md5 Hash with Salt for Keeping Password in Db in C#
Asp .Net MVC Disable Client Side Validation at Per-Field Level
How Expensive Is the Lock Statement
Return JSON with Error Status Code MVC
Automatic Native and Managed Dlls Extracting from Nuget Package
How to Create an Explorer-Like Folder Browser Control
How to Render a Wpf Usercontrol to a Bitmap Without Creating a Window
Get Value of C# Dynamic Property via String
C# How to Test a File Is a Jpeg