Hi
I have one problem with datagridview. I wanna to multiplying two cellse and displya result in third cell?
example:
A | B | C
10.50 | 2 | 21,00
5.20 | 4 | 20,80
7,3 | 5 | 36,50
after that i wanna to sum C column and display that in textBox.
Column A is type decimal
Column B is integer
Column C should be decimal too
how to do that and how to display decimal value in textbox?
I have found couple solutions on internet but they don't work for me.
ty
VulpesPosted May 30, 2013, 11:24 AM
decimal sum = 0.0m;
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
DataGridViewRow row = dataGridView1.Rows[i];
if (row.IsNewRow) break;
decimal product = (decimal)row.Cells[0].Value * (int)row.Cells[1].Value;
sum += product;
row.Cells[2].Value = product;
}
textBox1.Text = sum.ToString();
If there's any chance that some of the rows could contain null values, replace this line:
decimal product = (decimal)row.Cells[0].Value * (int)row.Cells[1].Value;
with this:
decimal product = Convert.ToDecimal(row.Cells[0].Value) * Convert.ToInt32(row.Cells[1].Value);
Zero will then be used for the null values.
Luka RoguljicPosted May 30, 2013, 12:24 PM
But thank you so much for this so far, ill try to figure it out !!
VulpesPosted May 30, 2013, 12:04 PM
The best thing to do is to perform the calculations on the rows in your DataTable, adding a new column if necessary, and then bind (or re-bind) to that.
Luka RoguljicPosted May 30, 2013, 11:55 AM
I did ask that question, but i thought that it will work with datagridview that gets data from database, should it work ??
This is the case: i have two datagridview, first gatagridview gets data from database. second datagridview gets values after I select row from first datagridview and add that row to second datagridview.
When i apply your code to my project i get this error:
in line >> decimal product = Convert.ToDecimal(row.Cells[1].Value) * Convert.ToInt32(row.Cells[2].Value);
argument out of range exception was unhandled