Click here to Skip to main content
15,890,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Like the following example:

id price price price total

1 25 10 30 ...

2 5 10 10 ...

3 10 20 10 ...

I want to sum of three columns like this in the DataGrid.

id price price price total

1 25 10 30 65

2 5 10 10 25

3 10 20 10 40


these three price from three tables ...i get the total by one button click

What I have tried:

i have tried this but it gives only first row total

int cell1 = Convert.ToInt32(dataGridView1.CurrentRow.Cells[2].Value);
int cell2 = Convert.ToInt32(dataGridView1.CurrentRow.Cells[3].Value);
int cell3 = Convert.ToInt32(dataGridView1.CurrentRow.Cells[4].Value);
int cell4 = Convert.ToInt32(dataGridView1.CurrentRow.Cells[5].Value);
int cell5 = Convert.ToInt32(dataGridView1.CurrentRow.Cells[6].Value);
int cell6 = Convert.ToInt32(dataGridView1.CurrentRow.Cells[7].Value);
int cell7 = Convert.ToInt32(dataGridView1.CurrentRow.Cells[8].Value);

if (cell1.ToString() != "" && cell2.ToString() != "" && cell3.ToString() != "" && cell4.ToString() != "" && cell5.ToString() != "" && cell6.ToString() != "" && cell7.ToString() != "")
{

dataGridView1.CurrentRow.Cells[0].Value = cell1 + cell2 + cell3 + cell4 + cell5 + cell6 + cell7;

}
Posted
Updated 1-Jun-18 6:03am
Comments
[no name] 1-Jun-18 9:08am    
So what issue you are facing on this?

1 solution

I'm assuming the code shown is in the button click handler. If so, your problem is that "CurrentRow" is only ever going to be the SINGLE row that is currently active.

While I do not think this is a good approach, the following would come close to your intent (in the question):

foreach (DataGridViewRow row in dataGridView1.Rows)
{
  int total = 0;
  for (int index = 1; index < row.Cells.Count; index++)
    total += Convert.ToInt32(row.Cells[index].Value);
  row.Cells[0].Value = total;
}


Beyond that, I'm not sure how to advise you. I'd need more information.

The user-interface you've chosen is a bit odd. You might consider something different, but then again perhaps you have an odd requirement :)

I would probably also suggest altering the list of objects or data table, backing the grid view, instead of directly altering the grid view itself. Then, you can re-trigger data binding.

However, here again I lack adequate information from your question to be certain this is the best advice.
 
Share this answer
 
Comments
Member 13854008 2-Jun-18 4:27am    
This code is not working,its shows an error(Input string was not in a correct format).
could u plz send me the code for me,its very urgent.....by one buttonclick i need full rows total at column "total"
Eric Lynch 2-Jun-18 9:05am    
As mentioned, there are almost certainly better approaches. However, without a better understanding of your goal, its difficult to provide the best advice. At a minimum, I'd need to see how you are populating the DataGridView, the data types of the columns, the names of the columns included in the total, and the name of the column to receive the total. Again, assuming you are populating the grid via DataSource, you are probably calculating the sums at the source instead of on the grid itself.


The example I provided simply totals all columns (except the first), it assumes all columns have a value that can be converted to an integer, and it deposits the total in the first column.


Complete source for the test harness I threw together (very quickly) is...


using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private List<myobject> values = new List<myobject>
{
new MyObject { Col2 = 1, Col3 = 2, Col4 = 3},
new MyObject { Col2 = 4, Col3 = 5, Col4 = 6},
new MyObject { Col2 = 7, Col3 = 8, Col4 = 9},
new MyObject { Col2 = 10, Col3 = 11, Col4 = 12}
};

private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.DataSource = values;
}

private class MyObject
{
public int Col1 { get; set; }

public int Col2 { get; set; }

public int Col3 { get; set; }

public int Col4 { get; set; }
}

private void button1_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
int total = 0;
for (int index = 1; index < row.Cells.Count; index++)
total += Convert.ToInt32(row.Cells[index].Value);
row.Cells[0].Value = total;
}
}
}
}

Assuming you want to total all columns except the one named "Col1" and deposit the result there, the button-click code would change as follows:

int totalIndex = 0;
for (int index = 0; index < dataGridView1.Columns.Count; index++)
if (dataGridView1.Columns[index].Name == "Col1")
{
totalIndex = index;
break;
}

foreach (DataGridViewRow row in dataGridView1.Rows)
{
int total = 0;
for (int index = 0; index < row.Cells.Count; index++)
if (index != totalIndex)
total += Convert.ToInt32(row.Cells[index].Value);

row.Cells[totalIndex].Value = total;
}

However, I'll stress again that more information is needed for me (or anyone) to provide a good response. If this doesn't help you, you'll need to either mark the problem as not solved (if this allows it) or re-post. I may be unavailable for a while, so someone else may need to pick up where I left off.
Member 13854008 4-Jun-18 0:54am    
sir ,this problem was solved.thanku very much for your reply sir ..your code was correct, error get bcoz of my mistake ur code was right one tanku very much

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