i want quantity * weight (both column's total) in the textbox in crystal report.
the data should retrive from sql database and should display in crystal reports.

3 Years
Discussion Span
Last Post by sagar.patole1

If the datagridview is backed by a datatable filled from something (a database query), then something like this would work.

// make a datatable to simulate a table filled from a database
DataTable dt = new DataTable();
DataRow r ;

dt.Columns.Add("item", typeof(string));
dt.Columns.Add("quantity", typeof(decimal));
dt.Columns.Add("weight", typeof(decimal));

r = dt.NewRow(); r[0] = "apples"; r[1] = 10; r[2] = 0.5; dt.Rows.Add(r);
r = dt.NewRow(); r[0] = "oranges"; r[1] = 20; r[2] = 0.6; dt.Rows.Add(r);
r = dt.NewRow(); r[0] = "bananas"; r[1] = 100; r[2] = 0.25; dt.Rows.Add(r);

// assume that dt was filled from a database with the 3 columns defined above
// you could add a total weight for item column
DataColumn twi = dt.Columns.Add("Tot_Weight", typeof(decimal));
// add an expression to the column to automatically do the multiplication
twi.Expression = "[quantity] * [weight]";

dataGridView1.DataSource = dt;

// if you do not want this column displayed then set it's visible property to false
dataGridView1.Columns["Tot_Weight"].Visible = true;

// add some formatting to the column
dataGridView1.Columns["Tot_Weight"].DefaultCellStyle.Format = "N3"; // 3 places after decimal mark
dataGridView1.Columns["Tot_Weight"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;

// now to get a total of the total weight column

decimal totweight = (from r2 in dt.AsEnumerable() 
                     select (decimal) r2["Tot_Weight"]

textBox1.Text = totweight.ToString();

// or without adding the total weight column
// and just do the calculation

decimal totweight2 = (from r2 in dt.AsEnumerable() 
                      select (decimal) r2["quantity"] * (decimal) r2["weight"]

but if i want total of the column in footer , i dont want another column for total.


can we do it in crystal reports

gold | total (quantity * weight)in grams
sunil | 25
manoj | 10
nilesh | 10

Edited by sagar.patole1: modified table

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.