[英]Exporting GridView to a datatable issue
I am trying to transfer grid-view data into data table.Then saving this data table to session.Why the output shown in this form 我试图将网格视图数据传输到数据表。然后将此数据表保存到session.Why输出以这种形式显示
Set gridview datasource in another page 在另一个页面中设置gridview数据源
GridView1.DataSource = (DataTable)Session["cart"];
GridView1.DataBind();
Out Put 出局
Pro Name Unit Price Quantity Total Amount
System.Web.UI.WebControls.GridViewRow
System.Web.UI.WebControls.GridViewRow
Code 码
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;
}
}
}
your help is much appertained 你的帮助很多
@Ayman, the problem is in the below mentioned part. @Ayman,问题在于下面提到的部分。 You provide the same variable for New Row. 您为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;
Change this by, 改变这个,
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;
you have created object for "GridViewRow" and "DataRow" with same name. 您已为同名的“GridViewRow”和“DataRow”创建了对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.