簡體   English   中英

將GridView導出為數據表問題

[英]Exporting GridView to a datatable issue

我試圖將網格視圖數據傳輸到數據表。然后將此數據表保存到session.Why輸出以這種形式顯示

在另一個頁面中設置gridview數據源

 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;
            }
        }
    }

你的幫助很多

@Ayman,問題在於下面提到的部分。 您為New Row提供相同的變量。

  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”和“DataRow”創建了對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM