Hello,
I created a gridView which has a label in the footer that calculates the total amount of the column's values.
the problem is when I clicked on " edit link " it shows me an error tells "
Object reference not set to an instance of an object."
in the line
amount += Convert.ToDecimal((e.Row.FindControl("Label3") as Label).Text);
C# codes
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[4]
{
new DataColumn("Invoice_ID", typeof(int)), new DataColumn("Invoice_Com"),
new DataColumn("Invoice_Type"), new DataColumn("Invoice_Amount", typeof(decimal))
});
dt.Rows.Add(1, "Invoice_Com 1", "1", "500");
dt.Rows.Add(2, "Invoice_Com 2", "2", "200");
dt.Rows.Add(3, "Invoice_Com 3", "3", "1500");
dt.Rows.Add(4, "Invoice_Com 4", "4", "789");
GridView1.DataBind();
}
}
decimal amount = 0;
protected void OnDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
amount += Convert.ToDecimal((e.Row.FindControl("Label3") as Label).Text);
}
if (e.Row.RowType == DataControlRowType.Footer)
{
(e.Row.FindControl("lblAmount") as Label).Text = amount.ToString();
}
}
protected void lbInsert_Click(object sender, EventArgs e)
{
ObjectDataSource1.InsertParameters["Invoice_Com"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TxtCom")).Text;
ObjectDataSource1.InsertParameters["Invoice_Type"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TxtType")).Text;
ObjectDataSource1.InsertParameters["Invoice_Amount"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TxtAmount")).Text;
ObjectDataSource1.InsertParameters["Invoice_ID"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TxtID")).Text;
ObjectDataSource1.Insert();
}
}