【C#】datatableにシーケンス番号列を追加し、番号を1から順次増やし、第1列に
1700 ワード
詳細リンク:https://shop499704308.taobao.com/?spm=a1z38n.10677092.card.11.594c1debsAGeak
///
/// DataTable , 1
///
/// DataTable
///
private DataTable AddSeriNumToDataTable(DataTable dt)
{
//
DataTable dtNew;
if (dt.Columns.IndexOf(" ") >= 0)
{
dtNew = dt;
}
else // ,
{
int rowLength = dt.Rows.Count;
int colLength= dt.Columns.Count;
DataRow[] newRows = new DataRow[rowLength];
dtNew = new DataTable();
// “ ”
dtNew.Columns.Add(" ");
for (int i = 0; i < colLength; i++)
{
dtNew.Columns.Add(dt.Columns[i].ColumnName);
// dt
for (int j = 0; j < rowLength; j++)
{
if (newRows[j] == null)
newRows[j] = dtNew.NewRow();
// ,
newRows[j][i+1] = dt.Rows[j][i];
}
}
foreach (DataRow row in newRows)
{
dtNew.Rows.Add(row);
}
}
// , 1
for (int i = 0; i < dt.Rows.Count; i++)
{
dtNew.Rows[i][" "] = i + 1;
}
return dtNew;
}