如何在中继器输出中每 5 条记录后插入一条空记录?



实际上我需要在中继器输出中每 5 条记录之后插入一条空记录。

例如 中继器原始输出如下 1 阿西夫 2 比拉尔 3 阿卜杜勒 4 阿里 5 巴巴尔 6 瓦卡斯 7 阿斯加尔

我们期望的输出如下 1 阿西夫 2 比拉尔 3 阿卜杜勒 4 阿里 5 巴巴尔

6 瓦卡斯 7 阿斯加尔

Aspx 页面代码为:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("CustomerName") %>'></asp:Label>
<br />
</ItemTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>

后端代码是:

SqlConnection con = new SqlConnection();
SqlCommand cmd = new SqlCommand();
protected void Page_Load(object sender, EventArgs e)
{
con.ConnectionString = "Data Source=PC\SQLEXPRESS;Initial Catalog=AlvisDB;Persist Security Info=True;User ID=sa;Password=pass";
con.Open();
cmd.Connection = con;
cmd.CommandText = "SELECT CustomerName FROM Customers";
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adp.Fill(ds);
Repeater1.DataSource = ds;
Repeater1.DataBind();
con.Close();
}
SqlConnection con = new SqlConnection();
SqlCommand cmd = new SqlCommand();
protected void Page_Load(object sender, EventArgs e)
{
con.ConnectionString = "Data Source=PC\SQLEXPRESS;Initial Catalog=AlvisDB;Persist Security Info=True;User ID=sa;Password=pass";
con.Open();
cmd.Connection = con;
cmd.CommandText = "SELECT CustomerName FROM Customers";
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adp.Fill(ds);

Repeater1.DataSource = ds;
Repeater1.DataBind();
con.Close();
}

只需将此代码添加到现有代码中即可

SqlConnection con = new SqlConnection();
SqlCommand cmd = new SqlCommand();
protected void Page_Load(object sender, EventArgs e)
{
con.ConnectionString = "Data Source=PC\SQLEXPRESS;Initial Catalog=AlvisDB;Persist Security Info=True;User ID=sa;Password=pass";
con.Open();
cmd.Connection = con;
cmd.CommandText = "SELECT CustomerName FROM Customers";
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adp.Fill(ds);
DataTable finalTable = new DataTable();
if (ds.Tables.Count > 0)
{
int i = 1;
DataTable firstTable = ds.Tables[0];
foreach (DataRow row in firstTable.Rows)
{
if (i == 5)
{
firstTable.NewRow();
i = 0;
}
finalTable.Rows.Add(row);
i++;
}
}
Repeater1.DataSource = finalTable;
Repeater1.DataBind();
con.Close();
}
protected void Page_Load(object sender, EventArgs e)
{
con.ConnectionString = "Data Source=PC\SQLEXPRESS;Initial Catalog=AlvisDB;Persist Security Info=True;User ID=sa;Password=pass";
con.Open();
cmd.Connection = con;
cmd.CommandText = "SELECT CustomerName FROM Customers";
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adp.Fill(ds);
if (ds != null && ds.Tables[0].Rows.Count > 4)  
{  
for (int i = 1; i < ds.Tables[0].Rows.Count - 1; i++)  
{  
var row = ds.Tables[0].NewRow();  
row["CustomerName"] = string.Empty;  
if (i % 5 == 0)  
{  
ds.Tables[0].Rows.InsertAt(row,i);  
ds.AcceptChanges();  
}  
}  
}
Repeater1.DataSource = finalTable;
Repeater1.DataBind();
con.Close();
}

这段代码帮助我实现期望的输出。

最新更新