c# - 父表單 - winform mdi form




如何將數據表按列分隔並存儲以備後用 (3)

DataTable(已經按Ee排序):

Title           EventDate                   EndDate 
Testing 1       3/23/2016 11:00:00 AM       3/23/2016 12:00:00 PM
Testing 2       3/23/2016 5:00:00 PM        3/23/2016 6:00:00 PM
Testing 9       3/24/2016 4:00:00 PM        3/24/2016 5:00:00 PM
Testing 5       3/29/2016 11:00:00 AM       3/29/2016 12:00:00 PM
Testing 6       3/29/2016 11:00:00 AM       3/29/2016 12:00:00 PM
Testing 4       3/29/2016 1:00:00 PM        3/29/2016 2:00:00 PM

ASP.net標籤:

<asp:Label ID="lDates" runat="server"></asp:Label>

我怎樣才能編碼,所以datatable顯示在這樣的標籤(它可能不是這樣,但有些時候我可以按照日期分開,並顯示在不同的地方):

3/23:   Testing 1
        Testing 2

3/24:   Testing 9

3/29:   Testing 5
        Testing 6
        Testing 4

在.aspx文件中:

<asp:Repeater ID="rptCalendar" runat="server" ClientIDMode="Static" OnItemDataBound="rptCalendar_ItemDataBound">
<HeaderTemplate>
    <div class="calEvParent widthFull hidOverflow" style="padding: 0 0 8px 0;">
</HeaderTemplate>
<ItemTemplate>
    <div class="calEvHolder width98 hidOverflow" style="height: 55px; background-color: rgb(255, 255, 255); box-shadow: 0px 0px 5px rgb(51, 51, 51); box-shadow: 0px 0px 5px rgba(51, 51, 51, 0.7); margin: 10px 0 0 1%;">
        <div class="calEvDateHolder floatL hidOverflow heightFull" style="width: 25%;">
            <div class="calEvDate widthFull heightFull hidOverflow dispIB textC">
                <span style="font-size: 32px; line-height: 1; font-weight: 300; font-family: 'Lato';"><%# Eval("EventDate", "{0:dd}") %></span>
                <br />
                <span style="font-size: 16px; line-height: 1; font-weight: 800; font-family: 'Lato';">
                    <%# Convert.ToDateTime(Eval("EventDate")).ToString("MMM") %>
                </span>
            </div>

        </div>
        <div style="float: left; width: 72%; height: 100%; padding: 0 0 0 2%; overflow: hidden;">
        <asp:DataList runat="server" ID="dtList">
            <ItemTemplate>
                <h2 style="font-size: 17px; font-weight: 700; margin: 0px; font-family: 'Lato';"><%# Eval("Title") %></h2>
                <span style="font-size: .6em;"><%# Eval("Location") %></span>
            </ItemTemplate>
        </asp:DataList>             
        </div>
    </div>
</ItemTemplate>
<FooterTemplate>
    </div>
</FooterTemplate>

並設置中繼器DataSource:

var groupedObjectList = from DataRow r in ds.Tables[0].Rows
                                group r by r.Field<DateTime>("EventDate") into g
                                select new { EventDate = g.Key, Items = g.Select(i => new { Title = i.Field<string>("Title"), Location = i.Field<string>("Location") }).ToList() };

        rptCalendar.DataSource = groupedObjectList;
        rptCalendar.DataBind();

和rptCalendar_ItemDataBound事件:

protected void rptCalendar_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item)
        {
            DataList itemList = ((DataList)e.Item.FindControl("dtList"));
            itemList.DataSource = ((dynamic)(e.Item.DataItem)).Items;
            itemList.DataBind();
        }
    }

我通過循環低谷DataTable ,獲取當前的EventDate並檢查它與以前的。

這是vb.net代碼:

    'get first date from table in format day.month - adapt to Your date format    
    Dim pDT As String = String.Format("{0:dd.MM}", dt.Rows(0)("EventDate"))
    Dim res As String = "<table cellspacing=0 cellpadding=0><tr><td style='text-align:left;vertical-align:top;border-bottom:solid 1px gray;'>" + pDT + "</td><td style='text-align:left;vertical-align:top;padding-bottom:10px;border-bottom:solid 1px gray;padding-left:10px;'>" + dt.Rows(0)("Title") + "<br>"
For x = 1 To dt.Rows.Count - 1
   'get EventDate from current row
   Dim nD As String = String.Format("{0:dd.MM}", dt.Rows(x)("EventDate"))
   If nD <> pDT Then
      res += "<td></tr>"
      pDT = String.Format("{0:dd.MM}", dt.Rows(x)("EventDate"))
      res += "<tr><td style='text-align:left;vertical-align:top;border-bottom:solid 1px gray;'>" + pDT + "</td><td style='text-align:left;vertical-align:top;padding-bottom:10px;border-bottom:solid 1px gray;padding-left:10px;'>" + dt.Rows(x)("Title") + "<br>"
   Else
      res += dt.Rows(x)("Title") + "<br>"
   End If
Next
res += "</td></tr></table>"
lDates.Text = res

由於我不使用c# ,下面的代碼被在線轉換器轉換(我希望這是正確的轉換:():

string pDT = string.Format("{0:dd.MM}", dt.Rows[0]["EventDate"]);
                string res = "<table cellspacing=0 cellpadding=0><tr><td style='text-align:left;vertical-align:top;border-bottom:solid 1px gray;'>" + pDT + "</td><td style='text-align:left;vertical-align:top;padding-bottom:10px;border-bottom:solid 1px gray;padding-left:10px;'>" + dt.Rows[0]["Title"] + "<br>";
                for (int x = 1; x <= dt.Rows.Count - 1; x++)
                {
                    string nD = string.Format("{0:dd.MM}", dt.Rows[x]["EventDate"]);
                    if (nD != pDT)
                    {
                        res += "<td></tr>";
                        pDT = string.Format("{0:dd.MM}", dt.Rows[(x]["EventDate"]);
                        res += "<tr><td style='text-align:left;vertical-align:top;border-bottom:solid 1px gray;'>" + pDT + "</td><td style='text-align:left;vertical-align:top;padding-bottom:10px;border-bottom:solid 1px gray;padding-left:10px;'>" + dt.Rows[x]["Title"] + "<br>";
                    }
                    else
                    {
                        res += dt.Rows[x]["Title"] + "<br>";
                    }
                }
                res += "</td></tr></table>";
                lDates.Text = res;

這是很髒的代碼。

更新(填充Repeater ):

然後,您必須創建新的空的DataTable並使用幾乎相同的代碼,但是將值放入string將它們放在新的DataTable (2列,一個用於日期,另一個用於標題):

代碼隱藏(vb.net):

Dim newDT As New DataTable
            newDT.Columns.Add("Date", GetType(String))
            newDT.Columns.Add("Titles", GetType(String))
            Dim pDT = String.Format("{0:dd.MM}", dt.Rows(0)("EventDate"))
            Dim titles As String = dt.Rows(0)("Title") + ","
            For x = 1 To dt.Rows.Count - 1
                Dim nD = String.Format("{0:dd.MM}", dt.Rows(x)("EventDate"))
                If pDT <> nD Then
                    titles = Mid(titles, 1, Len(titles) - 1)
                    newDT.Rows.Add({pDT, String.Join("<br>", titles.Split(",").ToArray)})
                    pDT = String.Format("{0:dd.MM}", dt.Rows(x)("EventDate"))
                    titles = dt.Rows(x)("Title") + ","
                Else
                    titles += dt.Rows(x)("Title") + ","
                End If
            Next
            titles = Mid(titles, 1, Len(titles) - 1)
            newDT.Rows.Add({pDT, String.Join("<br>", titles.Split(",").ToArray)})
            rptDate.DataSource = newDT
            rptDate.DataBind()

代碼隱藏( c# )(PS我不知道這是正確的轉換。對不起,再次,我非常微弱的c# :():

DataTable newDT = new DataTable();
newDT.Columns.Add("Date", typeof(string));
newDT.Columns.Add("Titles", typeof(string));
string pDT = string.Format("{0:dd.MM}", dt.Rows[0]["EventDate"]);
string titles = dt.Rows[0]("Title") + ",";
for (x = 1; x <= dt.Rows.Count - 1; x++) {
    string nD = string.Format("{0:dd.MM}", dt.Rows[0]["EventDate"]);
    if (pDT != nD) {
        titles = Strings.Mid(titles, 1, Strings.Len(titles) - 1);
        newDT.Rows.Add({
            pDT,
            string.Join("<br>", titles.Split(",").ToArray)
        });
        pDT = string.Format("{0:dd.MM}", dt.Rows[x]("EventDate"));
        titles = dt.Rows[x]("Title") + ",";
    } else {
        titles += dt.Rows[x]("Title") + ",";
    }
}
titles = Strings.Mid(titles, 1, Strings.Len(titles) - 1);
newDT.Rows.Add({
    pDT,
    string.Join("<br>", titles.Split(",").ToArray)
});
rptDate.DataSource = newDT;
rptDate.DataBind();

aspx:

<asp:Repeater runat="server" ID="rptDate">
            <HeaderTemplate>
                <table cellpadding="0" cellspacing="0">
            </HeaderTemplate>
            <ItemTemplate>
                <tr>
                    <td class="td1">
                        <%# Container.DataItem("Date")%>
                    </td>
                    <td class="td2">
                        <%# Container.DataItem("Titles")%>
                    </td>
                </tr>
            </ItemTemplate>
            <FooterTemplate>
                </table>
            </FooterTemplate>
        </asp:Repeater>

css:

.td1, .td2 {width:250px; border-bottom:solid 1px gray; text-align:left; vertical-align:top;}
.td1 {font-weight:bold;}
.td2 {font-style:italic;}

var groupedObjectList = from DataRow r in ds.Tables[0].Rows
                                  group r by r.Field<DateTime>("EventDate") into g
                                  select new { EventDate = g.Key, Items = g.ToList() };






datatable