Gridview Totals per Row
Trying to do something which should be fairly straight forward but racking my brains in trying to get it working. Essentially all i want to do is have a calculated column in a gridview which would be based on two values from the same row.
i.e. a Unit_Cost(money) and a rate(nvarchar(20)) column are bound to the DB but i want a Total cost to be made as a calculation from the 2. I'm using a BLL to retrieve the data from my DAL and believe this code should work - but it dont :(
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CropManagementDAL.TBL_RunRow run = (CropManagementDAL.TBL_RunRow)
((System.Data.DataRowView)e.Row.DataItem).Row;
if (!run.IsUnit_costNull())
{
if (!run.IsRateNull())
{
_totalUnitPrice = run.Unit_cost * Convert.ToInt16(run.Rate);
开发者_Python百科 e.Row.Cells[5].Text = _totalUnitPrice.ToString();
}
}
}
}
I get an error of "Input string was not in a correct format." and it highlights to my _totalUnitPrice = ... part of my code. I'm sure it's an easy one for one of you and your help is greatly appreciated!
If _totalUnitPrice
is a string, you need to convert the numeric value to a string before you pass it in.
One way to do that is this:
_totalUnitPrice = (run.Unit_cost * Convert.ToInt16(run.Rate)).ToString();
精彩评论