C# datatable, duplicate data
For testing I would like to expand my result set. I have a DataTable dt
that has 7 or so results开发者_运维问答. I would like to do something like:
dt.Rows.Add(dt);
a few times, just to make the data set larger.
I also tried dt.Rows.Add(dt.Rows[0]);
The first gave an error about the type, the second said the row already existed.
You need to do something like what's below. Basically generate a new row using the values from the existing row.
DataTable dt = new DataTable();
DataRow dr = dt.Rows[0];
dt.Rows.Add(dr.ItemArray);
You need to copy values to new row:
DataRow row = dt.NewRow();
row.ItemArray = dt.Rows[0].ItemArray;
dt.Rows.Add(row);
The first item fails because the function expects a DataRow
param.
The second item fails, because you are trying to add an item from the table, so it will inherently exist.
Try:
DataTable dt;
DataRow dr = dt.NewRow();
dr["field"] = "Some Value";
dt.Rows.Add(dr);
Check out the DataRow Class article on MSDN
精彩评论