html表格中间增加一行,C#如何在datatable已经存在的行中间插入新的一行

可以使用:dataTable.Rows.InsertAt(DataRow row, int position);

例子:

using System;

using System.Collections.Generic;

using System.Text;

using System.Data;

namespace ConsoleApplication1

{

class Program

{

static DataTable getDataTable()

{

DataTable table = new DataTable();

table.Columns.Add("userID", typeof(int));

table.Columns.Add("userName", typeof(string));

table.Columns.Add("isAwesome", typeof(bool));

return table;

}

static DataRow getRow(DataTable table, int userID, string userName, bool isAwesome)

{

DataRow row = table.NewRow();

row["userID"] = userID;

row["userName"] = userName;

row["isAwesome"] = isAwesome;

return row;

}

static void printTable(DataTable table)

{

foreach (DataRow row in table.Rows)

{

foreach (object val in row.ItemArray)

{

Console.Write("{0}, ", val);

}

Console.WriteLine();

}

}

static void Main(string[] args)

{

DataTable table = getDataTable();

table.Rows.Add(getRow(table, 1, "Juliet", true));

table.Rows.Add(getRow(table, 2, "Sean Hannity", false));

table.Rows.Add(getRow(table, 3, "Charles Darwin", true));

Console.WriteLine("Before:");

printTable(table);

// adding a row at index 1, between me and Sean Hannity

Console.WriteLine("------------\nAfter:");

DataRow barackRow = getRow(table, 4, "Barack Obama", true);

table.Rows.InsertAt(barackRow, 1);

printTable(table);

Console.Write("Press any key. . .");

Console.ReadKey(true);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值