DataTable在绑定到GridView时丢失了数据

时间:2013-09-18 07:22:23

标签: c# asp.net gridview datatable

我有一个有14列的gridview,其中一列用于ProductUID,这是不可见的,一个是AutoCompleteExtender的文本框。

我使用此文本框选择产品名称。 FillGridView函数填充所选产品的gridview。但问题是,当我添加新产品时,我首先调用AddNewRow()函数。它在gridview上添加了新行,然后调用fillgridview()函数。它用所选产品的详细信息填充所有列。

之后,当我在下一行添加另一个新产品时,它将丢失前一行,只有第一个单元格数据,其他列仍然存储。

我的源代码有什么问题?

请帮助我!

private void AddNewRow()
    {
        try
        {
            int rowIndex = 0;
            if (Session["CurrentTable"] != null)
            {
                DataTable dtCurrentTable = (DataTable)Session["CurrentTable"];
                DataRow drCurrentRow = null;

                if (dtCurrentTable.Rows.Count > 0)
                {
                    for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                    {
                        string boundfield0 = gvCounterSale.Rows[rowIndex].Cells[(int)CounterSaleColumnIndex.ProductID].Text;
                        TextBox TextBoxItemName =
                          (TextBox)gvCounterSale.Rows[rowIndex].Cells[(int)CounterSaleColumnIndex.ItemName].FindControl("item_txtBox");
                        string boundfield1 = gvCounterSale.Rows[rowIndex].Cells[(int)CounterSaleColumnIndex.Company].Text;
                        string boundfield2 = gvCounterSale.Rows[rowIndex].Cells[(int)CounterSaleColumnIndex.Packing].Text;
                        string boundfield3 = gvCounterSale.Rows[rowIndex].Cells[(int)CounterSaleColumnIndex.Unit].Text;
                        string boundfield4 = gvCounterSale.Rows[rowIndex].Cells[(int)CounterSaleColumnIndex.Expiry].Text;
                        string boundfield5 = gvCounterSale.Rows[rowIndex].Cells[(int)CounterSaleColumnIndex.MRP].Text;
                        string boundfield6 = gvCounterSale.Rows[rowIndex].Cells[(int)CounterSaleColumnIndex.ShelfNo].Text;
                        string boundfield7 = gvCounterSale.Rows[rowIndex].Cells[(int)CounterSaleColumnIndex.BatchNo].Text;
                        string boundfield8 = gvCounterSale.Rows[rowIndex].Cells[(int)CounterSaleColumnIndex.Stock].Text;
                        TextBox TextBoxQuantity =
                          (TextBox)gvCounterSale.Rows[rowIndex].Cells[(int)CounterSaleColumnIndex.Quantity].FindControl("qty_txtBox");
                        string boundfield9 = gvCounterSale.Rows[rowIndex].Cells[(int)CounterSaleColumnIndex.Amount].Text;
                        Button ButtonRemove =
                          (Button)gvCounterSale.Rows[rowIndex].Cells[(int)CounterSaleColumnIndex.Button].FindControl("btnRemove");

                        drCurrentRow = dtCurrentTable.NewRow();
                        drCurrentRow["RowNumber"] = i + 1;

                        dtCurrentTable.Rows[i - 1]["ProductID"] = boundfield0;
                        dtCurrentTable.Rows[i - 1]["Col1"] = TextBoxItemName.Text;
                        dtCurrentTable.Rows[i - 1]["Company"] = boundfield1;
                        dtCurrentTable.Rows[i - 1]["Packing"] = boundfield2;
                        dtCurrentTable.Rows[i - 1]["Unit"] = boundfield3;
                        dtCurrentTable.Rows[i - 1]["Expiry"] = boundfield4;
                        dtCurrentTable.Rows[i - 1]["Pur_Rate"] = boundfield5;
                        dtCurrentTable.Rows[i - 1]["Shelf_No"] = boundfield6;
                        dtCurrentTable.Rows[i - 1]["Batch_No"] = boundfield7;
                        dtCurrentTable.Rows[i - 1]["Stock"] = boundfield8;
                        dtCurrentTable.Rows[i - 1]["Col10"] = TextBoxQuantity.Text;
                        dtCurrentTable.Rows[i - 1]["Amount"] = boundfield9;
                        dtCurrentTable.Rows[i - 1]["Col11"] = ButtonRemove;
                        rowIndex++;
                    }
                    dtCurrentTable.Rows.Add(drCurrentRow);
                    Session["CurrentTable"] = dtCurrentTable;
                    gvCounterSale.DataSource = dtCurrentTable;
                    gvCounterSale.DataBind();
                }
            }
            else
            {
                Response.Write("Session is null");
            }
            SetPreviousData();
        }
        catch (Exception ex)
        {
            ScriptManager.RegisterStartupScript(Page, this.GetType(), "hi", "alert('Error Occured');", true);
        }
    }

 private void FillGridView()
    {
        try
        {
            Guid puid = new Guid();
            puid = System.Guid.ParseExact(hdnProductId.Value, "D");
            int rowIndex = gvCounterSale.Rows.Count - 1;
            DataTable dtCurrentTable = (DataTable)Session["CurrentTable"];

            string connStr = ConfigurationManager.ConnectionStrings["pharmacy_ConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(connStr);
            con.Open();
            SqlDataReader dr = null;

            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i < gvCounterSale.Rows.Count; i++)
                {
                    if (rowIndex != gvCounterSale.Rows.Count)
                    {
                        StringBuilder sb = new StringBuilder();
                        sb.Append(" SELECT STOCK.PRODUCT_ID, STOCK.BATCH_NO, STOCK.COMPANY_NAME, STOCK.EXPIRY, STOCK.PUR_RATE, STOCK.MRP, STOCK.SHELF_NO, ");
                        sb.Append(" STOCK.OPENING_STOCK, PRODUCT.PRODUCT_ID, PRODUCT.UNIT, PRODUCT.PACKING ");
                        sb.Append(" FROM STOCK INNER JOIN PRODUCT ");
                        sb.Append(" ON STOCK.PRODUCT_ID=PRODUCT.PRODUCT_ID WHERE PRODUCT.PRODUCT_ID = '" + puid + "'GROUP BY STOCK.PRODUCT_ID, STOCK.BATCH_NO, STOCK.COMPANY_NAME, ");
                        sb.Append("STOCK.EXPIRY, STOCK.PUR_RATE, STOCK.MRP, STOCK.SHELF_NO, STOCK.OPENING_STOCK, PRODUCT.PRODUCT_ID, PRODUCT.UNIT, PRODUCT.PACKING ");
                        SqlCommand cmdid = new SqlCommand(sb.ToString(), con);
                        dr = cmdid.ExecuteReader();
                        while (dr.Read())
                        {
                            gvCounterSale.Rows[rowIndex - 1].Cells[(int)CounterSaleColumnIndex.ProductID].Text = puid.ToString();
                            gvCounterSale.Rows[rowIndex - 1].Cells[(int)CounterSaleColumnIndex.Packing].Text = dr["PACKING"].ToString();
                            gvCounterSale.Rows[rowIndex - 1].Cells[(int)CounterSaleColumnIndex.Unit].Text = dr["UNIT"].ToString();
                            DateTime dt = Convert.ToDateTime(dr["EXPIRY"].ToString());
                            gvCounterSale.Rows[rowIndex - 1].Cells[(int)CounterSaleColumnIndex.Company].Text = dr["COMPANY_NAME"].ToString();
                            gvCounterSale.Rows[rowIndex - 1].Cells[(int)CounterSaleColumnIndex.Expiry].Text = dt.ToShortDateString();
                            gvCounterSale.Rows[rowIndex - 1].Cells[(int)CounterSaleColumnIndex.MRP].Text = dr["MRP"].ToString();
                            gvCounterSale.Rows[rowIndex - 1].Cells[(int)CounterSaleColumnIndex.ShelfNo].Text = dr["SHELF_NO"].ToString();
                            gvCounterSale.Rows[rowIndex - 1].Cells[(int)CounterSaleColumnIndex.BatchNo].Text = dr["BATCH_NO"].ToString();
                            gvCounterSale.Rows[rowIndex - 1].Cells[(int)CounterSaleColumnIndex.Stock].Text = dr["OPENING_STOCK"].ToString();
                        }
                        //rowIndex++;
                        dr.Close();
                    }
                }
                con.Close();
            }
        }
        catch (Exception ex)
        {
            ScriptManager.RegisterStartupScript(Page, this.GetType(), "hi", "alert('Error Occured');", true);
        }
    }

1 个答案:

答案 0 :(得分:0)

在这里你必须做点什么

class SessionHepler
         {
              public static DataTable OldTable
                 {
                  get;
                  set;
                  }
            }

//在你的代码中写这个----&gt; Session [“CurrentTable”] = dtCurrentTable;

SeeionHelper.OldTable = dtCurrentTable;

现在您将拥有旧的表值...因此您可以将其分配给当前表

喜欢

if(SeeionHelper.OldTable ==null)
      {
         dtCurrentTable = new DataTable();
        }
 else
   {
      dtCurrentTable = new DataTable();
      dtCurrentTable = SeeionHelper.OldTable;
    }

所以第一次OldTable将为null,它将从第二次创建,它会将OldTable分配给dtCurrentTable ...尝试这样