如下图所示,在某个日期有金额时,就在相应的表格中填充金额。
实现代码
Default4.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %> <!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"> table { border-collapse:collapse; } table,td,th{ border:1px solid #ccc; } td { 30px; } /*模拟对角线*/ .out{ border-top:40px #D6D3D6 solid;/*上边框宽度等于表格第一行行高*/ 0px;/*让容器宽度为0*/ height:0px;/*让容器高度为0*/ border-left:80px #BDBABD solid;/*左边框宽度等于表格第一行第一格宽度*/ position:relative;/*让里面的两个子容器绝对定位*/ } b{font-style:normal;display:block;position:absolute;top:-40px;left:-40px;35px;} em{font-style:normal;display:block;position:absolute;top:-25px;left:-70px;55x;} </style> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="btnTest" runat="server" Text="测试" OnClick="btnTest_Click" /><br /> <asp:Repeater runat="server" ID="rptList"> <HeaderTemplate> <table> <tr> <th style="80px"> <div class="out"> <b>日期</b> <em>月份</em> </div> </th> <th>1</th> <th>2</th> <th>3</th> <th>4</th> <th>5</th> <th>6</th> <th>7</th> <th>8</th> <th>9</th> <th>10</th> <th>11</th> <th>12 </th> <th>13</th> <th>14</th> <th>15</th> <th>16</th> <th>17</th> <th>18</th> <th>19</th> <th>20</th> <th>21</th> <th>22</th> <th>23</th> <th>24</th> <th>25</th> <th>26</th> <th>27</th> <th>28</th> <th>29</th> <th>30</th> <th>31</th> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td><%#Eval("D0")%></td> <td><%#Eval("D1")%></td> <td><%#Eval("D2")%></td> <td><%#Eval("D3")%></td> <td><%#Eval("D4")%></td> <td><%#Eval("D5")%></td> <td><%#Eval("D6")%></td> <td><%#Eval("D7")%></td> <td><%#Eval("D8")%></td> <td><%#Eval("D9")%></td> <td><%#Eval("D10")%></td> <td><%#Eval("D11")%></td> <td><%#Eval("D12")%></td> <td><%#Eval("D13")%></td> <td><%#Eval("D14")%></td> <td><%#Eval("D15")%></td> <td><%#Eval("D16")%></td> <td><%#Eval("D17")%></td> <td><%#Eval("D18")%></td> <td><%#Eval("D19")%></td> <td><%#Eval("D20")%></td> <td><%#Eval("D21")%></td> <td><%#Eval("D22")%></td> <td><%#Eval("D23")%></td> <td><%#Eval("D24")%></td> <td><%#Eval("D25")%></td> <td><%#Eval("D26")%></td> <td><%#Eval("D27")%></td> <td><%#Eval("D28")%></td> <td><%#Eval("D29")%></td> <td><%#Eval("D30")%></td> <td><%#Eval("D31")%></td> </tr> </ItemTemplate> <FooterTemplate> </table></FooterTemplate> </asp:Repeater> </div> </form> </body> </html>
Default4.aspx.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; public partial class Default4 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnTest_Click(object sender, EventArgs e) { DateTime dtStart = new DateTime(2013, 5, 31); DateTime dtEnd = new DateTime(2014, 5, 30); DataTable dt = GetDayData(GetData(), dtStart, dtEnd); rptList.DataSource = dt; rptList.DataBind(); } DataTable GetData() { DataTable dt = new DataTable(); DataRow dr; dt.Columns.Add(new DataColumn("Date", typeof(System.String))); dt.Columns.Add(new DataColumn("Money", typeof(System.Decimal))); dr = dt.NewRow(); dr[0] = new DateTime(2013, 5, 10).ToString("yyyyMMdd"); dr[1] = 200; dt.Rows.Add(dr); dr = dt.NewRow(); dr[0] = new DateTime(2013, 5, 30).ToString("yyyyMMdd"); dr[1] = 250; dt.Rows.Add(dr); dr = dt.NewRow(); dr[0] = new DateTime(2013, 5, 31).ToString("yyyyMMdd"); dr[1] = 290; dt.Rows.Add(dr); dr = dt.NewRow(); dr[0] = new DateTime(2013, 9, 8).ToString("yyyyMMdd"); dr[1] = 500; dt.Rows.Add(dr); dr = dt.NewRow(); dr[0] = new DateTime(2014, 5, 30).ToString("yyyyMMdd"); dr[1] = 880; dt.Rows.Add(dr); return dt; } DataTable GetDayData(DataTable dtData, DateTime dtStart, DateTime dtEnd) { DataTable dt = new DataTable(); DataRow dr; dt.Columns.Add(new DataColumn("D0", typeof(System.String))); dt.Columns.Add(new DataColumn("D1", typeof(System.String))); dt.Columns.Add(new DataColumn("D2", typeof(System.String))); dt.Columns.Add(new DataColumn("D3", typeof(System.String))); dt.Columns.Add(new DataColumn("D4", typeof(System.String))); dt.Columns.Add(new DataColumn("D5", typeof(System.String))); dt.Columns.Add(new DataColumn("D6", typeof(System.String))); dt.Columns.Add(new DataColumn("D7", typeof(System.String))); dt.Columns.Add(new DataColumn("D8", typeof(System.String))); dt.Columns.Add(new DataColumn("D9", typeof(System.String))); dt.Columns.Add(new DataColumn("D10", typeof(System.String))); dt.Columns.Add(new DataColumn("D11", typeof(System.String))); dt.Columns.Add(new DataColumn("D12", typeof(System.String))); dt.Columns.Add(new DataColumn("D13", typeof(System.String))); dt.Columns.Add(new DataColumn("D14", typeof(System.String))); dt.Columns.Add(new DataColumn("D15", typeof(System.String))); dt.Columns.Add(new DataColumn("D16", typeof(System.String))); dt.Columns.Add(new DataColumn("D17", typeof(System.String))); dt.Columns.Add(new DataColumn("D18", typeof(System.String))); dt.Columns.Add(new DataColumn("D19", typeof(System.String))); dt.Columns.Add(new DataColumn("D20", typeof(System.String))); dt.Columns.Add(new DataColumn("D21", typeof(System.String))); dt.Columns.Add(new DataColumn("D22", typeof(System.String))); dt.Columns.Add(new DataColumn("D23", typeof(System.String))); dt.Columns.Add(new DataColumn("D24", typeof(System.String))); dt.Columns.Add(new DataColumn("D25", typeof(System.String))); dt.Columns.Add(new DataColumn("D26", typeof(System.String))); dt.Columns.Add(new DataColumn("D27", typeof(System.String))); dt.Columns.Add(new DataColumn("D28", typeof(System.String))); dt.Columns.Add(new DataColumn("D29", typeof(System.String))); dt.Columns.Add(new DataColumn("D30", typeof(System.String))); dt.Columns.Add(new DataColumn("D31", typeof(System.String))); for (DateTime dtDay = dtStart; Convert.ToInt32(dtDay.ToString("yyyyMM")) <= Convert.ToInt32(dtEnd.ToString("yyyyMM")); dtDay = dtDay.AddMonths(1)) { dr = dt.NewRow(); for (int j = 0; j < 32; j++) { if (j == 0) { dr[j] = dtDay.ToString("yyyy年MM月"); } else { int daysInMonth = DateTime.DaysInMonth(dtDay.Year, dtDay.Month); if (j <= daysInMonth) { string dtCurrent = new DateTime(dtDay.Year, dtDay.Month, j).ToString("yyyyMMdd"); DataRow[] datarows = dtData.Select("Date='" + dtCurrent + "'"); if (datarows.Length > 0) { dr[j] = Convert.ToDecimal(datarows[0]["Money"]).ToString("N"); } } } } dt.Rows.Add(dr); } return dt; } }