Si8
Si8

Reputation: 9225

How to separate a DataTable by a column and store it for later use

DataTable (already sorted by 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 Label:

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

How can I code it so the datatable is displayed in the label like this (It might not be like this, but someway I can separate them by dates and display in different places):

3/23:   Testing 1
        Testing 2

3/24:   Testing 9

3/29:   Testing 5
        Testing 6
        Testing 4

Upvotes: 3

Views: 481

Answers (4)

Gabriel Ribeiro
Gabriel Ribeiro

Reputation: 33

in .aspx file:

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

And set repeater 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();

And rptCalendar_ItemDataBound Event:

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

Upvotes: 1

Ivan Stefanov
Ivan Stefanov

Reputation: 393

I do that by looping trough DataTable, get current EventDate and check it with previous on.

This is vb.net code :

    '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

Since I don't use c#, below code is converted by online converter (I hope so it's correct conversion :( ):

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;

It's little dirty code.

Update (populate Repeater) :

Then You have to create new, empty, DataTable and use, almost, same code, but, instead putting values into string put them, separately, in new DataTable (with 2 columns, one for date and another for Titles) :

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

codebehind (c#) (p.s. I'm not sure it's correct conversion. sorry, again, I'm very very weak with 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;}

Upvotes: 3

Gabriel Ribeiro
Gabriel Ribeiro

Reputation: 33

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

Upvotes: 1

Gabriel Ribeiro
Gabriel Ribeiro

Reputation: 33

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

Upvotes: 0

Related Questions