Add a column to a dataset in .net 1.1
I have dataset (dataset name dsEmp) with 25 columns, here I need to add a new column with columname of EndDate
of data type as string
.
I need to import all the data from column 18 whose datatype开发者_开发问答 is datetime
to the newly created datacolumn EndDate
. Once we import all the data from column 18 to the newcolumn EndDate
, we should remove the column 18.
Is this what you are looking for:
DataSet ds = new DataSet();
DataTable dtt = new DataTable();
ds.Tables.Add(dtt);
// simulate required columns
dtt.Columns.Add("col1", typeof(int));
dtt.Columns.Add("col2", typeof(string));
//...
dtt.Columns.Add("col18", typeof(DateTime));
// pupulate with dummy date
for (int index = 0; index < 100; index++)
{
dtt.Rows.Add(index, "val" + index.ToString(), DateTime.Now.AddMinutes(index));
}
// add new column
DataColumn colEndDate = new DataColumn("EndDate", typeof(DateTime));
dtt.Columns.Add(colEndDate);
// get old column reference
DataColumn colOld18 = dtt.Columns["col18"];
// loop thru all rows
foreach (DataRow row in dtt.Rows)
{
// store value from old column to new column
row[colEndDate] = Convert.ToDateTime(row[colOld18]).ToShortDateString();
// or
row[colEndDate] = Convert.ToDateTime(row[colOld18]).ToString("MM/dd/yyyy hh:mm:ss");
}
// remove old column
dtt.Columns.Remove(colOld18);
dtt.AcceptChanges();
@Prince I have prepared a small class for you so that you can us it to do the things you want. Hope it will be as per your requirement. Wanted to post this answer yesterday only but was busy in my work.
class TestAddRemoveCol { /// /// Adds the passed column to a datatable at a particular location /// /// /// /// /// public DataTable AddCol(DataSet ds, string colName, Type type, int location) { DataTable dt = new DataTable(); int colIndex = 0; DataColumn dc2 = null; foreach (DataColumn item in ds.Tables[0].Columns) { if (colIndex == location) { dc2 = new DataColumn(); dc2.ColumnName = colName; dc2.DataType = type; dt.Columns.Add(dc2); } dc2 = new DataColumn(); dc2.ColumnName = item.ColumnName; dc2.DataType = item.DataType; dt.Columns.Add(dc2); colIndex++; } foreach (DataRow dr in ds.Tables[0].Rows) { dt.ImportRow(dr); } return dt; } /// /// returns a datacolumn with type string /// /// /// /// public DataTable CopyCol(DataTable dt, string oldColName, string newColName) { DataTable dt2 = dt.Copy(); for (int i = 0; i /// Removes column from a particular location /// /// /// public void RemoveCol(DataTable dt, string colName) { //Remove column with a particular name dt.Columns.Remove(colName); //OR //Remove column at a particular index //ds.Tables[0].Columns.RemoveAt(index); } }
You can use this class as below.
//Test Add/Remove Col //Create dummy dataset for testing DataSet ds = new DataSet(); DataTable dt = new DataTable(); DataColumn dc = new DataColumn("col1", typeof(DateTime)); dt.Columns.Add(dc); dc = new DataColumn("col2", typeof(DateTime)); dt.Columns.Add(dc); dc = new DataColumn("col3", typeof(DateTime)); dt.Columns.Add(dc); dc = new DataColumn("col4", typeof(DateTime)); dt.Columns.Add(dc); DataRow dr = dt.NewRow(); dr[0] = Convert.ToDateTime("01/01/2011"); dr[1] = Convert.ToDateTime("02/01/2011"); dr[2] = Convert.ToDateTime("03/01/2011"); dr[3] = Convert.ToDateTime("04/01/2011"); dt.Rows.Add(dr); ds.Tables.Add(dt); //Create object of the class TestAddRemoveCol obj = new TestAddRemoveCol(); //Add column at the specific location in the dataset DataTable dt2 = obj.AddCol(ds, "EndDate", typeof(String), 2); //Copy data from one column to another DataTable dt3 = obj.CopyCol(dt2, "col3", "EndDate"); //Remove column with the specific name obj.RemoveCol(dt3, "col3");
I have checked the code and it works fine. Also this code will work in .net 1.1.
Regards,
Samar
精彩评论