Hello,

I need to bind a datatable stored in the session to a WebGrid control. I need to perform all edits on the table stored in the session and then post the final table to the data base once the user submits the form.

When ever I add a new row, two rows are getting added instead of one. I tried to delete the additional row but am getting an error saying that the row is not a part of the table.

Note: The first row is getting added successfully, this issue is repeated from 2nd row onwards.

Following is  the code that I am using:

protected void grid_InitializeDataSource(object sender, ISNet.WebUI.WebGrid.DataSourceEventArgs e)

    {

        DataSet.tblDetailsDataTable dtTable = new DataSet.tblDetailsDataTable();

        if ((DataSet.tblDetailsDataTable)(Session["Table"]) != null)

         

            e.DataSource = (DataSet.tblDetailsDataTable)(Session["Table"]);         

        else

            e.DataSource = dtTable;

 

    }

    protected void grid_PrepareDataBinding(object sender, ISNet.WebUI.WebGrid.DataSourceEventArgs e)

    {

        if (!IsPostBack)

        {

            grdExpenses.RetrieveStructure();   

        }

    }


protected void grid_AddRow(object sender, ISNet.WebUI.WebGrid.RowEventArgs e)

    {

 try

        {

            Dataset.tblDetailsDataTable dtTable  = new Dataset.tblDetailsDataTable();

            if ((Dataset.tblDetailsDataTable)(Session["Table"]) != null)

            {

              dtTable  = (Dataset.tblExpenseDetailsDataTable)(Session["Table"]);

            }

 

            DataRow dRow = dtTable .NewRow();

 

            dRow["EDID"] = 0;

            dRow["ESID"] = 0;

            dRow["Date"] = Convert.ToDateTime(e.Row.Cells[grdExpenses.RootTable.Columns.IndexOf("Date")].Text);

            dRow["ECID"] = e.Row.Cells[1].Value;

            dRow["Category"] = e.Row.Cells[1].Text;

            dRow["Description"] = e.Row.Cells[2].Value.ToString();

            dRow["Amount"] = Convert.ToDecimal(e.Row.Cells[3].Value);

 

            dtTable .Rows.Add(dRow);

 

            dtTable .AcceptChanges();

 

            if (((Dataset.tblExpenseDetailsDataTable)(Session["Table"])) == null || (((Dataset.tblExpenseDetailsDataTable)(Session["Table"])).Rows.Count == 0))

            {

                Session.Add("Table", dtTable );

            }

            else

            {

                Session["Table"] = dtTable ;

            }

 

            grid.ClearCachedDataSource();

            grid.RebindDataSource();

            grid.ClientAction.Refresh();

 

        }

        catch (Exception ex)

        {

            Response.Write(ex.Message);

        }

}