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.

Dani AI

Generated

A few practical options that avoid adding a visible "total" column and will work for both a runtime DataGridView total and for Crystal Reports (group or report totals). 's idea of computing at the data level is a good starting point and 's snippet points in the right direction — below are concise alternatives and common gotchas.

A quick client-side calc (no extra column): use the DataTable Compute method or sum the grid rows. Examples:

object sumObj = dataTable.Compute("Sum(quantity * weight)", "");
decimal total = (sumObj == DBNull.Value) ? 0m : Convert.ToDecimal(sumObj);
textBoxTotal.Text = total.ToString("N3");

or iterate grid rows if values may be edited in the UI:

decimal total = 0m;
foreach (DataGridViewRow r in dataGridView1.Rows)
{
    if (r.IsNewRow) continue;
    total += Convert.ToDecimal(r.Cells["quantity"].Value ?? 0) *
             Convert.ToDecimal(r.Cells["weight"].Value ?? 0);
}
textBoxTotal.Text = total.ToString("N3");

Server-side / Crystal-friendly approach: compute sums in SQL (better for large data or grouped totals) and bind the result to the report or display it in a textbox. Example SQL (SQL Server):

SELECT owner,
       SUM(ISNULL(quantity,0) * ISNULL(weight,0)) AS TotalWeight
FROM dbo.MyTable
GROUP BY owner;

In Crystal Reports you can also create a formula field like {@LineTotal} = {Table.Quantity} * {Table.Weight}, put it in Details, then use Insert → Summary on that formula to get the group/report total in the footer.

Troubleshooting notes: handle NULLs and use decimal/numeric types to avoid truncation; convert units explicitly (kg→g *1000) if needed; update totals on edits by responding to CellValueChanged/BindingSource events; for large result sets prefer DB aggregation rather than per-row loops for performance. — for per-person totals use the GROUP BY SQL example or Crystal group summaries as shown.

Recommended Answers

All 8 Replies

Welcome on this site! This little snipet might help you out.

ok thanks :)

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);
dt.AcceptChanges();


// 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"]
                    ).Sum();

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"]
                     ).Sum();

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

like this
col1
20
10

30

60

or
can we do it in crystal reports
like

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

please help me i am not getting the solution

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.