Animesh Ghosh
Animesh Ghosh

Reputation: 341

How to multiply data of two columns for third column in datagridview

I want to multiply the data of two columns and show it in the third column.

For example:

1st Column: Quantity
2nd Column: Rate
3rd Column: Price

I want to multiply as user enters the data for quantity and rate like Quantity=2, rate=50automatically in price column I want 100 to be appear.

Simultaneously I want to division as user enters the data for quantity and rate like Price=100, rate=50automatically in Quantity column I want 2 to be appear.

And when a user enters the data for quantity and rate like Price=100, Quantity=2automatically in Rate column I want 50 to be appear.

This three will happen in a same datagridview. User only enter any two field within this three and third will come automatically.

Using C#, VS2008, SQL 2008

private void dataGridView2_CellEndEdit(object sender, DataGridViewCellEventArgs e) 
{ 
    int quantity,rate; 
    for (int i = 0; i < dataGridView2.Rows.Count; i++) 
    { 
        if(int.TryParse(dataGridView2.Rows[i].Cells[1].Value.ToString(), out quantity) && int.TryParse(dataGridView2.Rows[i].Cells[2].Value.ToString(), out rate)) 
        { 
             int price = quantity * rate; dataGridView2.Rows[i].Cells[3].Value = price.ToString(); 
        } 
    } 
}

I write this code. There shows a error. Object reference not set to an instance of an object on

if(int.TryParse(dataGridView2.Rows[i].Cells[1].Value.ToString(), out quantity) && int.TryParse(dataGridView2.Rows[i].Cells[2].Value.ToString(), out rate)) 

this line.


enter image description here

I want to do like this. User can fill any 2 field among this 3. And third will take automatically.

Upvotes: 0

Views: 14307

Answers (4)

Nazir Rizwan
Nazir Rizwan

Reputation: 11

usually see error parameter column So dont use header name of column but only for column name as see

decimal qty = 0, price = 0, amount = 0;
for (int j = 0; j < dataGridView2.Rows.Count; ++j)
{
              // cloumn9=qty
  qty = Convert.ToDecimal(dataGridView2.Rows[j].Cells["Column9"].Value);
// column4 = price
  price = Convert.ToDecimal(dataGridView2.Rows[j].Cells["Column4"].Value);
  amount = qty * price;
// cloumn7 = amount
 dataGridView2.Rows[j].Cells["Column7"].Value = s13.ToString();

            }

Upvotes: 0

Ajitsing Rajput
Ajitsing Rajput

Reputation: 1

decimal s=0, s1=0,s13 = 0;
        for (int j = 0; j< dgvsaledetails.Rows.Count; ++j)
        {
            s = Convert.ToDecimal(dgvsaledetails.Rows[j].Cells[6].Value);
            s1 = Convert.ToDecimal(dgvsaledetails.Rows[j].Cells[10].Value);
            s13 = s * s1;
            dgvsaledetails.Rows[j].Cells["amount"].Value = s13.ToString();

        }

Upvotes: 0

Dulan Nadeeja
Dulan Nadeeja

Reputation: 1

This is the solution for you.

double s = Convert.ToDouble(DataGridShowAddedProducts.Rows[0].Cells[2].Value);
        double s1 = Convert.ToDouble(DataGridShowAddedProducts.Rows[0].Cells[2].Value);
        double s13 = s1 * s;
        MessageBox.Show(s13.ToString());

Upvotes: 0

Ehsan
Ehsan

Reputation: 32681

handle the CellEndEdit event of your gridview. And in that event calculate and assign the value to the third column

something like

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();
         }
    }

Upvotes: 2

Related Questions