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.

Recommended Answers

All 8 Replies

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

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.