Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 927
Si8 Avatar asked Dec 12 '25 03:12

Si8


2 Answers

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;}
like image 145
Ivan Stefanov Avatar answered Dec 13 '25 15:12

Ivan Stefanov


protected void rptTeste_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        ((Label)e.Item.FindControl("lblRepeater")).Text = ((DateTime)((dynamic)(e.Item.DataItem)).EventDate).ToString("MM/dd");
    }
like image 40
Gabriel Ribeiro Avatar answered Dec 13 '25 15:12

Gabriel Ribeiro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!