When i am clicking once time in on button to view data in gridview, it works fine, but when i click again on button, it is giving me error.
Object reference not set to an instance of an object.
on below line
DataTable dt = (e.Row.DataItem as DataRowView).DataView.Table;
string currentCategory = "";
decimal wbSubTotal = 0;
decimal bSubTotal = 0;
decimal WBalanceTotal = 0;
decimal balanceTotal = 0;
int rowIndex = 0;
protected void GVBBSTK_RowCreated1(object sender, GridViewRowEventArgs e)
{
wbSubTotal = 0;
bSubTotal = 0;
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataTable dt = (e.Row.DataItem as DataRowView).DataView.Table;
string category = dt.Rows[e.Row.RowIndex]["Supplier"].ToString();
balanceTotal += Convert.ToDecimal(dt.Rows[e.Row.RowIndex]["Balance"]);
WBalanceTotal += Convert.ToDecimal(dt.Rows[e.Row.RowIndex]["W_Balance"]);
if (category != currentCategory)
{
if (e.Row.RowIndex > 0)
{
for (int i = rowIndex; i < e.Row.RowIndex; i++)
{
wbSubTotal += Convert.ToDecimal(GVBBSTK.Rows[i].Cells[e.Row.Cells.Count - 1].Text);
bSubTotal += Convert.ToDecimal(GVBBSTK.Rows[i].Cells[e.Row.Cells.Count - 2].Text);
}
this.AddTotalRow("Sub Total", bSubTotal.ToString("N2"), wbSubTotal.ToString("N2"));
rowIndex = e.Row.RowIndex;
}
currentCategory = category;
}
}
}