Click here to Skip to main content
15,917,862 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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();


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.
Posted
Updated 28-May-12 2:40am
v4
Comments
Zoltán Zörgő 28-May-12 10:24am    
Do you really want to alter the table definition from code? Why? You can have a filed unused (not visible) in some queries, and used in others.
db7uk 28-May-12 17:50pm    
you are really better off doing this in SQL code using case statement. Much easier to bind to grid.
sahabiswarup 28-May-12 23:43pm    
i am creating a datatable in the cs page so why do i use sql rather using datatable add row/column.

Hi friend,

This below code may help you...


DataTable dt4 = new DataTable();
C#
dt4.Columns.Add("Eid");
       dt4.Columns.Add("Emp_Name");
       dt4.Columns.Add("uname");
       dt4.Columns.Add("Pwd");

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();
 
Share this answer
 
ALTER the original table to add the new column via sql,

SQL
'add column
ALTER TABLE [dbo].[pickupdroptariff] 
add  [Rate_New] [int]


then use UPDATE to fill in the new column for each row.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900