0

我有一个 gridview,它有 14 列,其中一列用于ProductUID,这是不可见的,一列是具有AutoCompleteExtender.

我使用此文本框来选择产品名称。FillGridView功能填充该选定产品的网格视图。但问题是,当我添加新产品时,我首先调用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);
        }
    }
4

1 回答 1

0

在这里你必须做点什么

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

// 在你的代码之前写下 ----> Session["CurrentTable"] = dtCurrentTable;

SeeionHelper.OldTable = dtCurrentTable;

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

喜欢

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

所以第一次 OldTable 将为 null 它将创建然后从第二次开始它将 OldTable 分配给 dtCurrentTable ...尝试这样

于 2013-09-18T08:46:03.663 回答