0

Possible Duplicate:
How to calculate sum of a DataTable's Column in LINQ (to Dataset)?

In my Data_Grid_View, I have one numeric column named Amount and I want to display total of this column in one text box ,can anyone tell what will be the code for it in c# .net

Community
  • 1
  • 1
cat20
  • 5
  • 1
  • 4

2 Answers2

1

Try this :

 int sum = 0;
 int ColumnIndex=1; //your column index 

 //Iterate through all the cells of Specified ColumnIndex in each row and get the sum 
 for (int i = 0; i < dataGridView1.Rows.Count; ++i)
 {
    sum += int.Parse(dataGridView1.Rows[i].Cells[ColumnIndex].Value.ToString());
 }
 //display sum 
 textBox1.Text = sum.ToString();
Sandeep Pathak
  • 10,567
  • 8
  • 45
  • 57
  • `textBox1.Text = sum.ToString()`! – Thorsten Dittmar Aug 04 '11 at 11:28
  • I have added your code on TextBox click private void textBox1_TextChanged(object sender, EventArgs e) { int sum = 0; int ColumnIndex = 2; //your column index //Iterate through all the cells of Specified ColumnIndex in each row and get the sum for (int i = 0; i < dataGridView1.Rows.Count; ++i) { sum += int.Parse(dataGridView1.Rows[i].Cells[ColumnIndex].Value.ToString()); } //display sum textBox1.Text = sum.ToString(); } – cat20 Aug 05 '11 at 06:34
  • Are you facing any problem with this...?? – Sandeep Pathak Aug 05 '11 at 06:47
  • I have added your code by clicking on text_box ,it is not showing me error but not also showing me sum in text_box,i should write this code on text_box click only right? – cat20 Aug 05 '11 at 07:02
  • You may want to write this code in some button_click event in order to test it ...!! – Sandeep Pathak Aug 05 '11 at 07:26
  • when i write it inbutton_click event it is throwing this Exception-These methods throw a FormatException if the supplied argument is not in a format that can be converted. Double.Parse will throw a FormatException if its string argument is not in a recognizable numeric format. – cat20 Aug 05 '11 at 16:42
1

Here's how you would do it with LINQ, assuming your amounts were double:

dataGridView.Rows.OfType<DataGridViewRow>()
            .Sum(row => Convert.ToDouble(row.Cells["Amount"].Value));

Here's how it would actually fit into your program:

private void button2_Click(object sender, EventArgs e)
{
    decimal sum = dataGridView1.Rows.OfType<DataGridViewRow>()
                  .Sum(row => Convert.ToDecimal(row.Cells["Money"].Value));
    textBox1.Text = sum.ToString();
}
Gabe
  • 84,912
  • 12
  • 139
  • 238
  • How do you get the Sum? When I type dataGridView.Rows. I cannot find "Sum" Anywhere and I tried searching the documentation. Also I'm already using System.Linq – MrFox Aug 04 '11 at 11:45
  • MrFox: Thanks for catching that. It needs `OfType() ` because `Rows` implements `IEnumerable` but not `IEnumerable` (which is required by LINQ). – Gabe Aug 04 '11 at 11:49
  • private void button2_Click(object sender, EventArgs e) { int sum = 0; dataGridView1.Rows.OfType() .Sum(row => Convert.ToDouble(row.Cells["Money"].Value)); textBox1.Text = sum.ToString(); } this is showing me 0 answer in text_box field ,wat should i do? – cat20 Aug 05 '11 at 06:59
  • @cat20: It's showing you 0 because you never put anything besides 0 into `sum`. See my edit for how to do it properly. – Gabe Aug 05 '11 at 11:53
  • Hey thank you so much it's working ,can you tell me if i directly want to show the sum in text box without having button then how should i do? – cat20 Aug 05 '11 at 16:46
  • Hey Thank you so much it's working ,can you tell me that if i want to show the sum directly in text box without having button than how can i do? – cat20 Aug 05 '11 at 16:49
  • @cat20: That's an entirely different question. You'll have to ask that separately. – Gabe Aug 05 '11 at 17:26