数据表生成GridView的使用 [英] Generating Datatable using GridView
本文介绍了数据表生成GridView的使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有网格视图,并在网格视图我有一个按钮和该按钮,我需要在数据表中插入/更新记录的选择。如果数据表,如果值是有那么数量字段将要加一。别的新行数量1被插入在数据表中。现在的事情是在GridView1_RowCommand我写这篇code。但它给我错误的值数据表。我的code下面写的。请帮帮我。
保护无效GridView1_RowCommand(对象发件人,GridViewCommandEventArgs E)
{ 如果(e.CommandName ==datacommand)
{ DataTable的DT =新的DataTable();
如果(会话[的product_id]!= NULL)
{
DT =(数据表)会议[的product_id];
}
DataRow的博士;
//dt.Rows[0][\"qty] =数据;
如果(dt.Rows.Count&下; = 0)
{
dt.Columns.Add(PRODUCT_ID的typeof(的Int32));
dt.Columns.Add(数量的typeof(INT));
dt.Columns.Add(价格的typeof(双));
dt.Columns.Add(总的typeof(双)); 博士= dt.NewRow();
博士[的product_id] = e.CommandArgument;
博士[数量] = 1;
博士[价格] = Convert.ToDouble(GridView1.Rows [0] .Cells [3]。文本);
博士[总] = Convert.ToInt32(DR [数量])* Convert.ToDouble(DR [价格]);
dt.Rows.Add(DR);
dt.AcceptChanges();
会议[的product_id] = DT;
的Response.Write(<脚本类型='javacript' - 酮时间与LT; / SCRIPT>中);
}
其他
{
// DT =会话[的product_id];
的for(int i = 0; I< dt.Rows.Count;我++)
{
如果(dt.Rows [I] [PRODUCT_ID]。的ToString()== e.CommandArgument)
{
博士= dt.NewRow();
dt.Rows [I] [数量] = Convert.ToInt32(dt.Rows [I] [数量])+ 1;
dt.Rows [I] [总] = Convert.ToInt32(dt.Rows [I] [数量])* Convert.ToDouble(dt.Rows [I] [价格]); 会议[的product_id] = DT;
dt.AcceptChanges();
的Response.Write(DT); }
}
博士= dt.NewRow();
博士[的product_id] = e.CommandArgument;
博士[数量] = 1;
博士[价格] = Convert.ToDouble(GridView1.Rows [0] .Cells [3]。文本);
博士[总] = Convert.ToInt32(DR [数量])* Convert.ToDouble(DR [价格]);
dt.Rows.Add(DR);
dt.AcceptChanges();
会议[的product_id] = DT; }
// GridViewRow排= e.CommandArgument
////的DataColumn PRODID =新的DataColumn(PRODUCT_ID的typeof(System.Int32));
////dt.Columns.Add(prodid);
//// DataColumn的数量=新的DataColumn(数量的typeof(System.Int32));
////dt.Columns.Add(qty);
// INT指数= Convert.ToInt32(e.CommandArgument);
// GridViewRow行= GridView1.Rows [指数] // AddShopCart(row.Cells [1] .Text.ToString());
}}
解决方案
保护无效GridView1_RowCommand(对象发件人,GridViewCommandEventArgs E)
{
如果(e.CommandName ==datacommand)
{ DataTable的DT =新的DataTable();
如果(会话[的product_id]!= NULL)
{
DT =(数据表)会议[的product_id];
}
DataRow的博士; 如果(dt.Rows.Count&下; = 0)
{
dt.Columns.Add(PRODUCT_ID的typeof(的Int32));
dt.Columns.Add(数量的typeof(INT));
dt.Columns.Add(价格的typeof(双));
dt.Columns.Add(总的typeof(双));
博士= dt.NewRow();
博士[的product_id] = e.CommandArgument;
博士[数量] = 1;
博士[价格] = Convert.ToDouble(GridView1.Rows [0] .Cells [3]。文本);
博士[总] = Convert.ToInt32(DR [数量])* Convert.ToDouble(DR [价格]);
dt.Rows.Add(DR);
dt.AcceptChanges();
会议[的product_id] = DT;
的Response.Write(<脚本类型='javacript' - 酮时间与LT; / SCRIPT>中);
}
其他
{ 字符串AA =新;
的for(int i = 0; I< dt.Rows.Count;我++)
{
如果(dt.Rows [I] [PRODUCT_ID]。的ToString()== e.CommandArgument)
{
AA =DUP; }
}
如果(AA ==DUP)
{
对于(INT J = 0; J< dt.Rows.Count; J ++)
{
如果(dt.Rows [J] [PRODUCT_ID]。的ToString()== e.CommandArgument)
{
// AA =DUP;
dt.Rows [J] [数量] = Convert.ToString(Convert.ToInt32(dt.Rows [J] [数量])+ 1);
dt.AcceptChanges();
}
}
会议[的product_id] = DT;
}
其他
{
dt.Columns.Add(PRODUCT_ID的typeof(的Int32));
dt.Columns.Add(数量的typeof(INT));
dt.Columns.Add(价格的typeof(双));
dt.Columns.Add(总的typeof(双)); DataRow的DR1 = dt.NewRow();
DR1 [的product_id] = e.CommandArgument;
DR1 [数量] = 1;
DR1 [价格] = Convert.ToDouble(GridView1.Rows [0] .Cells [3]。文本);
DR1 [总] = Convert.ToInt32(DR [数量])* Convert.ToDouble(DR [价格]);
dt.Rows.Add(DR);
dt.AcceptChanges();
会议[的product_id] = DT;
}
}
}
}
所以这就是答案
I am having Grid view and in that grid view i am having one button and on selection of that button i need to insert/update record in data table. If in data table if value is there then the qty field will gonna increase by one. Else new row with qty 1 is inserted in the data table. Now the thing is in GridView1_RowCommand i am writing this code. But it is giving me wrong values in data table. My code is written below. Please help me.
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "datacommand")
{
DataTable dt = new DataTable();
if (Session["product_id"] != null)
{
dt = (DataTable)Session["product_id"];
}
DataRow dr;
//dt.Rows[0]["qty"] = data;
if (dt.Rows.Count<=0)
{
dt.Columns.Add("product_id", typeof(Int32));
dt.Columns.Add("qty", typeof(int));
dt.Columns.Add("price", typeof(double));
dt.Columns.Add("total", typeof(double));
dr = dt.NewRow();
dr["product_id"] = e.CommandArgument;
dr["qty"] = 1;
dr["price"] = Convert.ToDouble(GridView1.Rows[0].Cells[3].Text);
dr["total"] = Convert.ToInt32(dr["qty"]) * Convert.ToDouble(dr["price"]);
dt.Rows.Add(dr);
dt.AcceptChanges();
Session["product_id"] = dt;
Response.Write("<script type='javacript'> One time</script>");
}
else
{
//dt = Session["product_id"];
for (int i = 0; i < dt.Rows.Count; i++)
{
if (dt.Rows[i]["product_id"].ToString() == e.CommandArgument)
{
dr = dt.NewRow();
dt.Rows[i]["qty"] = Convert.ToInt32(dt.Rows[i]["qty"])+ 1;
dt.Rows[i]["total"] = Convert.ToInt32(dt.Rows[i]["qty"]) * Convert.ToDouble(dt.Rows[i]["price"]);
Session["product_id"] = dt;
dt.AcceptChanges();
Response.Write(dt);
}
}
dr = dt.NewRow();
dr["product_id"] = e.CommandArgument;
dr["qty"] = 1;
dr["price"] = Convert.ToDouble(GridView1.Rows[0].Cells[3].Text);
dr["total"] = Convert.ToInt32(dr["qty"]) * Convert.ToDouble(dr["price"]);
dt.Rows.Add(dr);
dt.AcceptChanges();
Session["product_id"] = dt;
}
//GridViewRow row= e.CommandArgument
////DataColumn prodid = new DataColumn("product_id", typeof(System.Int32));
////dt.Columns.Add(prodid);
////DataColumn qty = new DataColumn("qty", typeof(System.Int32));
////dt.Columns.Add(qty);
//int index = Convert.ToInt32(e.CommandArgument);
//GridViewRow row = GridView1.Rows[index];
//AddShopCart(row.Cells[1].Text.ToString());
}
}
解决方案
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "datacommand")
{
DataTable dt = new DataTable();
if (Session["product_id"] != null)
{
dt = (DataTable)Session["product_id"];
}
DataRow dr;
if (dt.Rows.Count<=0)
{
dt.Columns.Add("product_id", typeof(Int32));
dt.Columns.Add("qty", typeof(int));
dt.Columns.Add("price", typeof(double));
dt.Columns.Add("total", typeof(double));
dr = dt.NewRow();
dr["product_id"] = e.CommandArgument;
dr["qty"] = 1;
dr["price"] = Convert.ToDouble(GridView1.Rows[0].Cells[3].Text);
dr["total"] = Convert.ToInt32(dr["qty"]) * Convert.ToDouble(dr["price"]);
dt.Rows.Add(dr);
dt.AcceptChanges();
Session["product_id"] = dt;
Response.Write("<script type='javacript'> One time</script>");
}
else
{
string aa="new";
for (int i = 0; i < dt.Rows.Count; i++)
{
if (dt.Rows[i]["product_id"].ToString() == e.CommandArgument)
{
aa="dup";
}
}
if(aa=="dup")
{
for (int j = 0; j < dt.Rows.Count; j++)
{
if (dt.Rows[j]["product_id"].ToString() == e.CommandArgument)
{
// aa="dup";
dt.Rows[j]["qty"]=Convert.ToString( Convert.ToInt32( dt.Rows[j]["qty"])+1);
dt.AcceptChanges();
}
}
Session["product_id"]=dt;
}
else
{
dt.Columns.Add("product_id", typeof(Int32));
dt.Columns.Add("qty", typeof(int));
dt.Columns.Add("price", typeof(double));
dt.Columns.Add("total", typeof(double));
DataRow dr1=dt.NewRow() ;
dr1["product_id"] = e.CommandArgument;
dr1["qty"] = 1;
dr1["price"] = Convert.ToDouble(GridView1.Rows[0].Cells[3].Text);
dr1["total"] = Convert.ToInt32(dr["qty"]) * Convert.ToDouble(dr["price"]);
dt.Rows.Add(dr);
dt.AcceptChanges();
Session["product_id"] = dt;
}
}
}
}
so this is the answer
这篇关于数据表生成GridView的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文