"If at first you don't succeed; call it version 1.0" :-Unknown

Pages

Saturday, December 1, 2012

Dynamically add a new column to dataset or data table in c#



Dynamically add a new column to dataset or data table in c#
 

System.Data.DataTable dtIncremented = ds.Tables[0];
        DataColumn auto = new DataColumn("AutoID", typeof(System.Int32));
        dtIncremented.Columns.Add(auto);
        //auto.AutoIncrement = true;
        //auto.AutoIncrementSeed = 1;
        //auto.ReadOnly = true;
        //auto.Unique = true;

       // dtIncremented.Columns.Add(auto);
        //dtIncremented.PrimaryKey = new DataColumn[] { auto };

        dtIncremented.BeginLoadData();

        DataTableReader dtReader = new DataTableReader(ds.Tables[0]);
        dtIncremented.Load(dtReader);

        dtIncremented.EndLoadData();

//foreach (DataRow dr in dtIncremented.Rows)
       // {
       //     dr["AutoID"] = dtIncremented.Rows.IndexOf(dr);
     //   }


If u had any trouble just ask, Happy to help u :)
Stay Tune...
Have a nice day... 'N happy Coding :)

No comments: