Click here to Skip to main content
15,898,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello.
I have column there is quantity, price and commonPrice. i want to fill this commonPrice with price * quantity value.
How can i do this?
Posted
Comments
T.Saravanann 23-Oct-10 4:01am    
Are you enter the Quantity & Price value in DataGridView Cell? (or) its come from Database means just refer Mohd Wasif Answer.
lester555 23-Oct-10 4:36am    
Grid gets price value from sql databese and quantity from txtbox
Pawan Kiran 23-Oct-10 5:00am    
How you are going to Calculate CommonPrice Cells in DataGridView?
Means something like u are providing any Control To Calculate All Values!

If u are Entering Values in GridView Cells.

Handle Datagridview1_CellValueChanged Event
Exa: 
private void dgrid_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
     if(e.RowIndex>=0)
     {
        if(!string.IsnullOrEmpty(dgrid.Rows[e.RowIndex].Cells["quantity"].Value.ToString()) && !string.IsnullOrEmpty(dgrid.Rows[e.RowIndex].Cells["Price"].Value.ToString())
            {
               dgrid.Rows[e.RowIndex].Cells["commonPrice"].Value = Convert.ToDouble(dgrid.Rows[e.RowIndex].Cells["quantity"].Value) *
Convert.ToDouble(dgrid.Rows[e.RowIndex].Cells["Price"].Value)
                //Ensure That Quantity and Price Should be Digits Only.  
            }
     }
}
vote if it is useful one.
 
Share this answer
 
v2
Hi Lester,

First you get the Price value into DataTable and then to use the following Code

dtData --> DataTable Object

dtData.Columns.Add("CommonPrice");

for(int ix=0;ix<dtData.Rows.Count;ix++)
{
   dtData.Rows[ix]["CommonPrice"]=((decimal.Parse(dtData.Rows[ix]["Price"]))*(int.Parse(TextBox1.Text))).ToString();
}

DataGridView1.DataSource=dtData.Copy();


Try this code.

Cheers :)
 
Share this answer
 
Please clarify.

U would be fetching data from table if yes then

you u have bind this query with gridview.

as

select quantity,price ,quantity*price as commonPrice from tablename
 
Share this answer
 
v2
See here[^]. Notice how this is done using a bound column in this case.
This can also be achieved using an unbound column.
 
Share this answer
 
try to use this

convert the column to Template then do like this
<br />
<asp:label id="lblSubTotatl" runat="server" text='<%# (decimal)Eval("Price")*(decimal)Eval("quantity")%>' > <br />
<br />
<br />
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900