I have a grid view which displays some values. now i have to calculate row wise sum of those values for each row and then display against them.
i tried this code but i am getting error as Input String Was not in Correct Format.
public void gv_RowCreated(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int total = Convert.ToInt32(e.Row.Cells[1].Text) + Convert.ToInt32(e.Row.Cells[2].Text) + Convert.ToInt32(e.Row.Cells[3].Text)开发者_如何学JAVA + Convert.ToInt32(e.Row.Cells[4].Text) + Convert.ToInt32(e.Row.Cells[5].Text) + Convert.ToInt32(e.Row.Cells[6].Text) + Convert.ToInt32(e.Row.Cells[7].Text);
((Label)gv.FindControl("Label8")).Text = Convert.ToString(total);
}
}
Try by setting the following code to Item Template of Label8:
<asp:TemplateField HeaderText="Col8">
<ItemTemplate>
<%# Convert.ToInt32(Eval("col1")) + Convert.ToInt32(Eval("col2")) + ... %>
</ItemTemplate>
</asp:TemplateField>
</asp:TemplateField>
Check whether it works fine.
I am assuming from your question that you want to sum the values in your rows, not the values in your columns (which is more usual).
Your grid will be bound to an IEnumerable list of objects, and the properties on those objects will form the columns of the grid.
The simple way to do this is to have one more property on your data object, and that property can be a sum of the other properties - so you have a calculated property.
However you will probably want this property to be shown as the last column in the grid, to do this reliably (in a non-hack way) you will need to specifically declare the columns, i.e. you will have to set AutoGenerateColumns
to false.
Edit:
after looking at your code i would have to say that is a bit of an ugly way to do it (and processing grid rows in this way is very slow). A better way to do this is to include the calculated property in your data object and keep the calculation encapsulated there, as your grid is merely for display and doesn't need to have any knowledge about the values themselves.
Here is an example data object to give you an idea of what i mean, i've mixed up the data types a bit just to make it a bit more complex:
public class MyDataObject
{
public int MyValue1 { get; set; }
public string MyValue2 { get; set; }
public decimal MyValue3 { get; set; }
public decimal MyTotal
{
get
{
return MyValue1
+ MyValue3
+ (string.IsNullOrWhiteSpace(MyValue2) ? 0 : Convert.ToInt32(MyValue2));
}
}
//alternate version:
public decimal MyTotal2
{
get
{
return MyValue1 + MyValue3 + ConvertToInt(MyValue2, 0);
}
}
//helper method:
private int ConvertToInt(string input, int defaultVal)
{
int temp = 0;
if (int.TryParse(input, out temp))
return temp;
return defaultVal;
//this can also be written more succinctly as:
//return int.TryParse(input, out temp) ? temp : defaultVal;
}
}
use this code for that,
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblTotal = (Label)e.Row.FindControl("lblTotal");
int iCount;
if (Decimal.TryParse(lblTotal.Text, iCount) ) {
iCount += iCount;
}
}
if (e.Row.RowType == DataControlRowType.Footer)
{
Label lblTotalAmt = (Label)e.Row.FindControl("lblTotalAmt");
lblTotalAmt.Text = iCount.ToString();
}
}
精彩评论