在重复器输出中的每 5 条记录后插入一个空记录的方法是什么?

huangapple go评论58阅读模式
英文:

How to insert one empty record after every 5 records in the repeater output?

问题

I am need to insert one empty record after every 5 records in the repeater output.

For example, Repeater original output is following:
1 Asif
2 Bilal
3 Abdul
4 Ali
5 Babar
6 Waqas
7 Asghar

Our desired output is following:
1 Asif
2 Bilal
3 Abdul
4 Ali
5 Babar

6 Waqas
7 Asghar

Aspx page code is:

<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>

And backend code is:

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();
}
英文:

actually i am need to insert one empty record after every 5 records in the repeater output.

For example
Repeater original output is following
1 Asif
2 Bilal
3 Abdul
4 Ali
5 Babar
6 Waqas
7 Asghar

Our desired output is following
1 Asif
2 Bilal
3 Abdul
4 Ali
5 Babar

6 Waqas
7 Asghar

Aspx page code is:

&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head runat=&quot;server&quot;&gt;
    &lt;title&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;form id=&quot;form1&quot; runat=&quot;server&quot;&gt;
        &lt;div&gt;
            &lt;asp:Repeater ID=&quot;Repeater1&quot; runat=&quot;server&quot;&gt;
                &lt;ItemTemplate&gt;
                    &lt;asp:Label ID=&quot;Label1&quot; runat=&quot;server&quot; Text=&#39;&lt;%#Eval(&quot;CustomerName&quot;) %&gt;&#39;&gt;&lt;/asp:Label&gt;
                    &lt;br /&gt;
                &lt;/ItemTemplate&gt;
            &lt;/asp:Repeater&gt;
        &lt;/div&gt;
    &lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;

And backend code is:

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

答案1

得分: 3

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 = &quot;Data Source=PC\\SQLEXPRESS;Initial Catalog=AlvisDB;Persist Security Info=True;User ID=sa;Password=pass&quot;;
            con.Open();
            cmd.Connection = con;
            cmd.CommandText = &quot;SELECT CustomerName FROM Customers&quot;;
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            adp.Fill(ds);


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

just add this code in your existing code

            SqlConnection con = new SqlConnection();
            SqlCommand cmd = new SqlCommand();
            protected void Page_Load(object sender, EventArgs e)
            {
                con.ConnectionString = &quot;Data Source=PC\\SQLEXPRESS;Initial Catalog=AlvisDB;Persist Security Info=True;User ID=sa;Password=pass&quot;;
                con.Open();
                cmd.Connection = con;
                cmd.CommandText = &quot;SELECT CustomerName FROM Customers&quot;;
                SqlDataAdapter adp = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                adp.Fill(ds);
                DataTable finalTable = new DataTable();
                if (ds.Tables.Count &gt; 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();
            }

答案2

得分: 0

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();
}
This piece of code helps me achieve the desired output.
英文:
    protected void Page_Load(object sender, EventArgs e)
                {
                    con.ConnectionString = &quot;Data Source=PC\\SQLEXPRESS;Initial Catalog=AlvisDB;Persist Security Info=True;User ID=sa;Password=pass&quot;;
                    con.Open();
                    cmd.Connection = con;
                    cmd.CommandText = &quot;SELECT CustomerName FROM Customers&quot;;
                    SqlDataAdapter adp = new SqlDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    adp.Fill(ds);
    if (ds != null &amp;&amp; ds.Tables[0].Rows.Count &gt; 4)  
    {  
        for (int i = 1; i &lt; ds.Tables[0].Rows.Count - 1; i++)  
        {  
            var row = ds.Tables[0].NewRow();  
            row[&quot;CustomerName&quot;] = string.Empty;  
                        
            if (i % 5 == 0)  
            {  
                ds.Tables[0].Rows.InsertAt(row,i);  
                ds.AcceptChanges();  
            }  
        }  
    }
Repeater1.DataSource = finalTable;
                Repeater1.DataBind();
                con.Close();
}

This piece of code helps me it achieving the desires output.

huangapple
  • 本文由 发表于 2020年1月3日 19:37:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577944.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定