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 ;
r = dt.NewRow(); r = "apples"; r = 10; r = 0.5; dt.Rows.Add(r);
r = dt.NewRow(); r = "oranges"; r = 20; r = 0.6; dt.Rows.Add(r);
r = dt.NewRow(); r = "bananas"; r = 100; r = 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"]
Help! I want to create a java program that finds the highest even integer among the values entered by the user. Stop asking values when a value less than 1 have been entered. If no even integer is entered, display "No Even Integer"
Hi, as I was told that my code doesn’t scale well at all, I thought perhaps I’d try to get a better understanding of interfaces/abstract classes and classes and the relationship between them.
I don’t want at this stage work on a big separate project as I've already got plenty ...