将GridView导出到数据表问题

本文关键字:数据表 问题 GridView | 更新日期: 2024-10-23 19:09:14

我正在尝试将网格视图数据传输到数据表中。然后将此数据表保存到会话。为什么输出以这种形式显示

在另一个页面中设置网格视图数据源

 GridView1.DataSource = (DataTable)Session["cart"];
 GridView1.DataBind();

输出

 Pro Name                             Unit Price    Quantity    Total Amount
System.Web.UI.WebControls.GridViewRow            
System.Web.UI.WebControls.GridViewRow   

代码

    foreach (GridViewRow row in GvProducts.Rows)
    {
        if (row.RowType == DataControlRowType.DataRow)
        {
            CheckBox chkRow = (row.Cells[0].FindControl("chkSel") as CheckBox);
            if (chkRow.Checked)
            {
                string proid = row.Cells[1].Text;
                string balance = row.Cells[3].Text;
                string proname = row.Cells[2].Text;
                string proqty = (row.Cells[5].FindControl("txtQuantity") as TextBox).Text;
                string UnitPrice = row.Cells[6].Text;
                DataTable tbl;
                if (Session["cart"] == null)
                {
                    tbl = new DataTable();
                    tbl.Columns.Add("Pro Name");
                    tbl.Columns.Add("Unit Price");
                    tbl.Columns.Add("Quantity");
                    tbl.Columns.Add("Total Amount");
                }
                else
                tbl = (DataTable)Session["cart"];
                DataRow row = tbl.NewRow();
                row[0] = proname;
                row[1] = Convert.ToDecimal(UnitPrice);
                row[2] = proqty;
                row[3] = Convert.ToInt32(proqty) * Convert.ToDecimal(UnitPrice);
                tbl.Rows.Add(row);
                Session["cart"] = tbl;
            }
        }
    }

你的帮助与有关

将GridView导出到数据表问题

@Ayman,问题在下面提到的部分。为"新行"提供相同的变量。

  DataRow row = tbl.NewRow();
                row[0] = proname;
                row[1] = Convert.ToDecimal(UnitPrice);
                row[2] = proqty;
                row[3] = Convert.ToInt32(proqty) * Convert.ToDecimal(UnitPrice);
                tbl.Rows.Add(row);
                Session["cart"] = tbl;

更改依据,

DataRow newRow = tbl.NewRow();
                    newRow[0] = proname;
                    newRow[1] = Convert.ToDecimal(UnitPrice);
                    newRow[2] = proqty;
                    newRow[3] = Convert.ToInt32(proqty) * Convert.ToDecimal(UnitPrice);
                    tbl.Rows.Add(newRow);
                    Session["cart"] = tbl;

您已经为"GridViewRow"answers"DataRow"创建了具有相同名称的对象。