问题描述
我已经从这样的数据库中生成了一个数据表
来源目的地车辆费率
-------------------------------------------------- -
a b XXX 500
a c xxx 400
在数据表中添加一列(New_Rate)并分配值后.
来源目的地车辆费率新价格
-------------------------------------------------- ------------------------------
a b XXX 500 1000
a c xxx 400800
这是我的代码段:
i have generated a datatable from database like this
source destination vehicle Rate
---------------------------------------------------
a b XXX 500
a c xxx 400
After add a column (New_Rate) in the datatable and assign value.
source destination vehicle Rate New_Rate
--------------------------------------------------------------------------------
a b XXX 500 1000
a c xxx 400 800
Here is my code snippet:
SqlDataAdapter adp2 = new SqlDataAdapter("select source, destination, vehicle, rate from pickupdroptariff order by source", conn);
DataTable dt2 = new DataTable();
adp2.Fill(dt2);
dt2.Columns.Add("Rate_New");
int veh_rate=0;
if (dt2.Rows.Count > 0)
{
for (int j = 0; j < dt2.Rows.Count; j++)
{
if(dt2.Rows[j][2].ToString()=="Small Car")
{
veh_rate = Convert.ToInt32(dt2.Rows[j][3].ToString());
veh_rate = veh_rate + small;
//DataRow row = null;
//row[4] = veh_rate.ToString();
//dt2.Rows.Add(row);
dt2.Rows.Add("small",veh_rate.ToString());
}
if(dt2.Rows[j][2].ToString()=="Regular Car")
{
veh_rate = Convert.ToInt32(dt2.Rows[j][3].ToString());
veh_rate = veh_rate + regular;
dt2.Rows.Add("Regular", veh_rate.ToString());
}
if(dt2.Rows[j][2].ToString()=="Luxury Car")
{
veh_rate = Convert.ToInt32(dt2.Rows[j][3].ToString());
veh_rate = veh_rate + luxury;
dt2.Rows.Add("Luxury", veh_rate.ToString());
}
}
}
GridView1.DataSource = dt2;
GridView1.DataBind();
其中:dt2
是数据表,veh_rate
存储车辆费率
但是新记录将添加到先前记录的下面.
请帮助我,非常感谢.
Where:dt2
is the datatable,veh_rate
stores vehicle rate
But new records are added below of the previous records.
Please help me, thank you very much.
推荐答案
dt4.Columns.Add("Eid");
dt4.Columns.Add("Emp_Name");
dt4.Columns.Add("uname");
dt4.Columns.Add("Pwd");
foreach(Gv.Rows中的GridViewRow行)
{
dr3 = dt4.NewRow();
标签Eid =(标签)row.FindControl("lblEid");
TextBox Ename =(TextBox)row.FindControl("txtEmpname");
TextBox Uname =(TextBox)row.FindControl("txtUname");
TextBox pwd =(TextBox)row.FindControl("txtPwd");
dr3 [0] = Eid.Text;
dr3 [1] = Ename.Text;
dr3 [2] = Uname.Text;
dr3 [3] = pwd.Text;
dt4.Rows.Add(dr3);
}
dr3 = dt4.NewRow();
dt4.Rows.Add(dr3);
dt.Clear();
Session.Remove("dt");
dt = dt4.Copy();
会话["dt"] = dt4;
Gv.DataSource = dt4;
Gv.DataBind();
foreach (GridViewRow row in Gv.Rows)
{
dr3 = dt4.NewRow();
Label Eid = (Label)row.FindControl("lblEid");
TextBox Ename = (TextBox)row.FindControl("txtEmpname");
TextBox Uname = (TextBox)row.FindControl("txtUname");
TextBox pwd = (TextBox)row.FindControl("txtPwd");
dr3[0] = Eid.Text;
dr3[1] = Ename.Text;
dr3[2] = Uname.Text;
dr3[3] = pwd.Text;
dt4.Rows.Add(dr3);
}
dr3 = dt4.NewRow();
dt4.Rows.Add(dr3);
dt.Clear();
Session.Remove("dt");
dt = dt4.Copy();
Session["dt"] = dt4;
Gv.DataSource = dt4;
Gv.DataBind();
'add column
ALTER TABLE [dbo].[pickupdroptariff]
add [Rate_New] [int]
然后使用UPDATE为每一行填充新列.
then use UPDATE to fill in the new column for each row.
这篇关于如何在Datable中创建新列并将值分配给新创建的列行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!