简体   繁体   English

动态添加行到表的错误

[英]Bug in adding rows to table on the fly

i try adding rows to table on the fly from DataBase but its always the last row that appears only where am i wrong ?我尝试从数据库动态添加行到表,但它总是最后一行只出现在我错在哪里?


            TableCell pNameCell = new TableCell();
            TableCell pDescCell = new TableCell();
            TableCell pPriceCell = new TableCell();
            TableCell pStockCell = new TableCell();
            TableCell buyProduct = new TableCell();
            HyperLink hl = new HyperLink();

//ds is DataSet
            foreach (DataRow dRow in ds.Tables[0].Rows)
            {
                TableRow row = new TableRow();
                pNameCell.Text = dRow["name"].ToString();
                row.Cells.Add(pNameCell);
                pDescCell.Text = dRow["description"].ToString();
                row.Cells.Add(pDescCell);
                pPriceCell.Text = dRow["price"].ToString();
                row.Cells.Add(pPriceCell);
                pStockCell.Text = dRow["Qty"].ToString();
                row.Cells.Add(pStockCell);
                hl.Text = "Add To Cart";
                hl.NavigateUrl = "BuyProduct.aspx?id="+ dRow["pro_id"].ToString();
                hl.CssClass = "btn btn-primary";
                buyProduct.Controls.Add(hl);
                row.Cells.Add(buyProduct);
//TProducts is asp:table ID
                TProducts.Rows.Add(row);
            }

it should show all the data rows in able它应该显示所有数据行

Your table cells are not unique to each iteration of the loop.您的表格单元格对于循环的每次迭代都不是唯一的。 When you add a cell variable to a row, it maintains a reference to that variable, not a copy.将单元格变量添加到行时,它会维护对该变量的引用,而不是副本。 So then in the next iteration of the loop you overwrite the cell variable with the value from the new row, this will also update all the references to that cell.因此,在循环的下一次迭代中,您用新行中的值覆盖单元格变量,这也将更新对该单元格的所有引用。

To fix it, simply move the table cell declarations inside the loop, then their scope will be limited to that iteration, and new variables will be created each time you loop - just like the table row variable already is, in fact:要修复它,只需将表格单元格声明移动到循环内,然后它们的范围将被限制在该迭代中,并且每次循环时都会创建新变量 - 就像表格行变量实际上已经是这样:

//ds is DataSet
foreach (DataRow dRow in ds.Tables[0].Rows)
{
  TableCell pNameCell = new TableCell();
  TableCell pDescCell = new TableCell();
  TableCell pPriceCell = new TableCell();
  TableCell pStockCell = new TableCell();
  TableCell buyProduct = new TableCell();
  HyperLink hl = new HyperLink();
  TableRow row = new TableRow();

  pNameCell.Text = dRow["name"].ToString();
  row.Cells.Add(pNameCell);
  pDescCell.Text = dRow["description"].ToString();
  row.Cells.Add(pDescCell);
  pPriceCell.Text = dRow["price"].ToString();
  row.Cells.Add(pPriceCell);
  pStockCell.Text = dRow["Qty"].ToString();
  row.Cells.Add(pStockCell);
  hl.Text = "Add To Cart";
  hl.NavigateUrl = "BuyProduct.aspx?id="+ dRow["pro_id"].ToString();
  hl.CssClass = "btn btn-primary";
  buyProduct.Controls.Add(hl);
  row.Cells.Add(buyProduct);
  //TProducts is table ID
  TProducts.Rows.Add(row);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM