根据记录分组、将数据动态纵向显示

10 篇文章 0 订阅
10 篇文章 0 订阅

效果图:样式还需进一步设置;



前台:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DemoList.aspx.cs" Inherits="WebApplication1.DemoList" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
    </style>
</head>
<body>
    
   <div>
        <table id="table1" runat="server" width="780px">
            <tr>
                <td style="width:100%;height:20px" align="left">供应商信息</td>
            </tr>
            <tr>
                <td style=" border:1px solid">
                    <asp:Literal ID="Literal1" runat="server"></asp:Literal>
                </td>
            </tr>
        </table>
   </div>
</body>
</html>


后台:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Text;
namespace WebApplication1
{
    public partial class DemoList : System.Web.UI.Page
    {


        string strConnection="Data Source=LENOVOPC\\SQLEXPRESS;initial catalog=TeachingLib;user id=sa;password=123";
        string sql = "select regNo,company,currency,exchange,priceOne,priceTwo,number from SupplierInfo";
        
        protected void Page_Load(object sender, EventArgs e)
        {
            ShowData();
        }
        private string[] GetFields()
        {
            return new string[] { "推荐", "公司名", "币种", "汇率", "单价(第一轮)", "单价(最终)", "数量" };
        }
        private void ShowData()
        {
            string chk="";
            DataTable dt=this.CreateTable();
            int rows=dt.Rows.Count;
            if(rows>0)             //表示有数据的情况下;
            {
                StringBuilder sbt = new StringBuilder();
                sbt.Append("<table id='tContent'>");
                string[] strs=GetFields();


                sbt.Append("<tr><td><table>");
                int group = rows / 3;
                if (rows % 3!=0)    //表示前3的倍数表;
                {
                    if(group==0)         //表示等于一组(3个)的数据;一组中列数小于3列;
                    {
                        for (int index1 = 0; index1 < strs.Length; index1++)   //表示一共7行数据;这个是标题字段;
                        {
                            sbt.Append("<tr><td width:25%>" + strs[index1] + "</td></tr>");
                        }
                        sbt.Append("</table></td>");
                       
                        for (int index2 = 0; index2 < rows; index2++) //少于3列的数据;
                        {
                            sbt.Append("<td><table>");
                            for (int index3 = 0; index3 < strs.Length; index3++)
                            {
                                if (index3 == 0)
                                {
                                    chk="<input id='Checkbox1' type='checkbox' disabled='disabled'";
                                    chk=dt.Rows[index2][index3].ToString().Trim() == "1"? chk+" checked='checked'/>":chk+"/>"; 
                                    sbt.Append("<tr><td>" + chk + "</td></tr>");
                                }
                                else
                                {
                                    sbt.Append("<tr><td>" + dt.Rows[index2][index3].ToString() + "</td></tr>");
                                } 
                            }
                               
                            sbt.Append("</table></td>");
                        }
                        sbt.Append("</tr></table>");
                        this.Literal1.Text = sbt.ToString();
                    }
                    else                   //表示多于一组(3个)数据;
                    {
                        for(int index4=0;index4<group;index4++)   //表示大于一组的数据,需要列出的组数;group为最后一组;
                        {
                            for (int index1 = 0; index1 < strs.Length; index1++)   //表示一共7行数据;这个是标题字段;
                            {
                                sbt.Append("<tr><td width:25%>" + strs[index1] + "</td></tr>");
                            }
                            sbt.Append("</table></td>");
                            for (int index5 = index4 * 3; index5 < index4 * 3+3; index5++)
                            {
                                sbt.Append("<td><table>");
                                for (int index1 = 0; index1 < strs.Length; index1++)   //表示一共7行数据;这个是标题字段;
                                {
                                    if (index1 == 0)
                                    {
                                        chk = "<input id='Checkbox1' type='checkbox' disabled='disabled'";
                                        chk = dt.Rows[index5][index1].ToString().Trim() == "1" ? chk + " checked='checked'/>" : chk + "/>";
                                        sbt.Append("<tr><td>" + chk + "</td></tr>");
                                    }
                                    else 
                                    {
                                        sbt.Append("<tr><td width:25%>" + dt.Rows[index5][index1].ToString() + "</td></tr>");
                                    }
                                }
                                sbt.Append("</table></td>");
                            }
                            sbt.Append("</tr><tr><td style='width:100%;height:20px'></td></tr><tr><td><table>");
                        }


                        for (int index1 = 0; index1 < strs.Length; index1++)   //表示一共7行数据;这个是标题字段;
                        {
                            sbt.Append("<tr><td width:25%>" + strs[index1] + "</td></tr>");
                        }
                        sbt.Append("</table></td>");


                        for (int index6 = group * 3; index6 < rows; index6++)  //表示剩余的行数,即转变成相应的列数;
                        {
                            sbt.Append("<td><table>");
                            for (int index3 = 0; index3 < strs.Length; index3++)
                            {
                                if (index3 == 0)
                                {
                                    chk = "<input id='Checkbox1' type='checkbox' disabled='disabled'";
                                    chk = dt.Rows[index6][index3].ToString().Trim() == "1" ? chk + " checked='checked'/>" : chk + "/>";
                                    sbt.Append("<tr><td>" + chk + "</td></tr>");
                                }
                                else
                                {
                                    sbt.Append("<tr><td>" + dt.Rows[index6][index3].ToString() + "</td></tr>");
                                }
                            }
                            sbt.Append("</table></td>");
                        }
                        sbt.Append("</tr></table>");
                        this.Literal1.Text = sbt.ToString();
                    }
                }
                else                     //刚好被3整除的表;
                {
                    for(int index7=0;index7<group;index7++)
                    {
                        for (int index1 = 0; index1 < strs.Length; index1++)   //表示一共7行数据;这个是标题字段;
                        {
                            sbt.Append("<tr><td width:25%>" + strs[index1] + "</td></tr>");
                        }
                        sbt.Append("</table></td>");
                        for (int index5 = index7 * 3; index5 < index7 * 3+3; index5++)
                        {
                            sbt.Append("<td><table>");
                            for (int index1 = 0; index1 < strs.Length; index1++)   //表示一共7行数据;这个是标题字段;
                            {
                                if(index1==0)
                                {
                                    chk = "<input id='Checkbox1' type='checkbox' disabled='disabled'";
                                    chk = dt.Rows[index5][index1].ToString().Trim() == "1" ? chk + " checked='checked'/>" : chk + "/>";
                                    sbt.Append("<tr><td>" + chk + "</td></tr>");
                                }
                                else
                                {
                                    sbt.Append("<tr><td width:25%>" + dt.Rows[index5][index1].ToString() + "</td></tr>");
                                }
                            }
                            sbt.Append("</table></td>");
                        }
                        if (index7==(group-1))
                        {
                            sbt.Append("</tr></table>");
                        }
                        else
                        {
                            sbt.Append("</td></tr><tr><td style='width:100%;height:20px'></td></tr><tr><td><table>");
                        }
                    }
                    this.Literal1.Text = sbt.ToString();
                }
            }
        }


        private DataTable CreateTable()
        {
            DataTable dt=new DataTable();
            SqlConnection cn = new SqlConnection(strConnection);
            cn.Open();
            SqlCommand cmm = new SqlCommand(sql, cn);
            SqlDataAdapter adapter = new SqlDataAdapter(cmm);
            adapter.Fill(dt);
            return dt;
        }
    }


数据库:





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值