如何通过列分隔DataTable并将其存储以供以后使用

时间:2016-03-23 15:10:46

标签: c# asp.net datatable

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>

我如何对其进行编码,以便数据表在标签中显示如下(可能不是这样,但我可以按日期将它们分开并在不同的地方显示):

3/23:   Testing 1
        Testing 2

3/24:   Testing 9

3/29:   Testing 5
        Testing 6
        Testing 4

4 个答案:

答案 0 :(得分:3)

我通过循环播放app.use(function(req, res, next) { res.header("Access-Control-Allow-Origin", "*"); // or res.header("Access-Control-Allow-Origin", "localhost"); res.header("Access-Control-Allow-Headers", "*"); next(); }); ,获取当前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中(与2列,一列用于日期,另一列用于标题):

codebehind(vb.net):

DataTable

codebehind(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() )(p.s.我不确定它是否正确转换。抱歉,再次,我对c#非常弱:():

c#

aspx:

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();

css:

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

答案 1 :(得分:1)

protected void rptTeste_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        ((Label)e.Item.FindControl("lblRepeater")).Text = ((DateTime)((dynamic)(e.Item.DataItem)).EventDate).ToString("MM/dd");
    }

答案 2 :(得分:1)

<。>在.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();
        }
    }

答案 3 :(得分:0)

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