Click here to Skip to main content
15,894,410 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
i have filled datatable from db and want to insert a new column in datatable and fill it by hand.


C#
public virtual DataTable cart(ArrayList dt)
        {
            int loop = 0;
            DataTable result = new DataTable();
            SqlCommand cmd;
            DataTable dtrow = new DataTable();
            while (loop < dt.Count)
            {
                cmd = new SqlCommand("getcart @prname");
                cmd.Parameters.AddWithValue("@prname", dt[loop].ToString());
                dtrow = data.getdata(ref cmd);
                result.Merge(dtrow);
                loop++;
            }
            DataColumn qty = new DataColumn ("qty");
            result.Columns.Add(qty);
            DataRow dr;
            foreach (DataRow row in result.Rows)
            {
                dr = result.NewRow();
                dr["qty"] = 1;
                result.Rows.Add(dr);
            }

           
            
            return result;
        }
Posted

 
Share this answer
 
below 3 lines will add column with value 1
C#
System.Data.DataColumn newColumn = new System.Data.DataColumn("qty", typeof(int));
newColumn.DefaultValue = 1;
result.Columns.Add(newColumn);
 
Share this answer
 
Comments
abid zahid 8-Jun-14 9:49am    
Thanks
DamithSL 8-Jun-14 10:09am    
you are welcome! :)

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