Mai
Mai

Reputation: 13

how to calculate datagridview column?

I am a new user in C# winform and also new user in this site, too

I have a datagridview with 4 columns

      No.     proname           price        qty      total

      1        fish               0.0         1         -
      2        tofu               0.0         1         -

Data in example like that, I give the default price and qty like above total is empty. Now I need to cellclick on column price to new input value on currentrow after I gave the new value I leave it by press Enter key or anyway just leave the cell I need the price*qty = total on currentrow. Or opposite I new input the qty to 2... and price not "0.0" then Price * qty=total

But if I edit on other cell not two of above the event do nothing.

How do I multiply that ?

Anybody can help me in better solution?

Thank you in advance.

Upvotes: 0

Views: 178

Answers (2)

GGesheva
GGesheva

Reputation: 186

Here is exactly what you're looking for. Answer

The answer given by this guy is really well explained and it's not going to be hard doing the implementation.

By this way the "Total" column is automatically maintained via an Expression which means you cannot manually do anything to that column.

Upvotes: 0

Hitesh Anshani
Hitesh Anshani

Reputation: 1549

In the code Behind use this

int.Parse(row.Cells[3].Value.toString()) * int.Parse(row.Cells[4].Value.toString())

Your Method look like this

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        int quantity,rate;
        if (int.TryParse(dataGridView1.Rows[e.RowIndex].Cells["quantity"].Value.ToString(), out quantity) && int.TryParse(dataGridView1.Rows[e.RowIndex].Cells["rate"].Value.ToString(), out rate))
        {
            int price = quantity * rate;
            dataGridView1.Rows[e.RowIndex].Cells["price"].Value = price.ToString();
         }
    }

For more find reference here

Another Reference

Upvotes: 1

Related Questions