private DataTable InsertABlankRow(DataTable dt)
{
int n = dt.Rows.Count;
DataTable dtnew = new DataTable();
if (dt.Columns.Contains("colname"))
{
var countRows = dt.Select("colname ='xyz'").Length;
if (countRows > 1)
{
foreach (DataRow drow in dt.Rows)
{
if (drow["colname"].ToString() == "xyz")
{
int index = dt.Rows.IndexOf(drow);
dt.Rows.InsertAt(dt.NewRow(), index + 1);
dt.AcceptChanges();
}
}
}
}
}
Modified code:
private DataTable Add_a_Row(DataTable dtnew)
{
DataTable dt = new DataTable();
dt= dtnew.Clone();
foreach (DataRow dr in dtnew.Rows)
{
dt.ImportRow(dr);
if(dr["colname"].ToString()=="xyz")
{
dt.Rows.Add(dt.NewRow());
}
}
return dt;
}
Hi All, I am trying to insert blank rows at multiple places where ever the condition is met. For the first time when the condition is met a new empty row is added but for the second one it throws an exception, collection changed. Is there a way to do this in a much better way. Thank you...