locked
RowCreated shows error on posback RRS feed

  • Question

  • User1052662409 posted

    Hi All,

    I am trying to show subtotal in gridview group by dates. Here I used gridview's RowCreated  event,

    When I binding the gridview it's working fine, but when I try to add another row to grid by clicking a button it shows null reference error on postback.

     protected void GVCash_RowCreated(object sender, GridViewRowEventArgs e)
        {
            subTotal_cash = 0;
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
               
                DataTable dt = (e.Row.DataItem as DataRowView).DataView.Table;
                DateTime date = Convert.ToDateTime(dt.Rows[e.Row.RowIndex]["date"]);
                total_cash += Convert.ToDecimal(dt.Rows[e.Row.RowIndex]["opening_balance"]);
                if (date != currentId_cash)
                {
                    if (e.Row.RowIndex > 0)
                    {
                        for (int i = subTotalRowIndex_cash; i < e.Row.RowIndex; i++)
                        {
                            subTotal_cash += Convert.ToDecimal(GVCash.Rows[i].Cells[1].Text);
                        }
                        this.AddTotalRow_cash(subTotal_cash.ToString());
                        subTotalRowIndex_cash = e.Row.RowIndex;
                    }
                    currentId_cash = date;
                }
            }
        }

    On this DataTable dt = (e.Row.DataItem as DataRowView).DataView.Table; line it shows the null reference error.

    Pls suggest.

    Saturday, June 9, 2018 6:44 PM

All replies

  • User-330142929 posted

    Hi Mukesh1,

    When I binding the gridview it's working fine, but when I try to add another row to grid by clicking a button it shows null reference error on postback.

    According to your description, when the page is resubmitted to the server, if you do not perform a rebind  function in page_load, it is flushed as soon as it refreshes, or you do not put the Gridview binding event in the If(!ispostback), the row_created event  parameter will get a null reference.

    So I suggest you bind the data in page_load, just like this:

     

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    this.GridView1.DataSource = GetDataTable();
                    this.GridView1.DataBind();
                }
            }

    If the solution could not solve your problem, please feel free to let me know.

     

    Best Regards,

    Abraham

    Monday, June 11, 2018 1:52 PM