texto tabla convertir c# asp.net datatable

c# - convertir tabla en texto word 2010



Cómo separar una tabla de datos por una columna y almacenarla para su uso posterior (4)

Lo hago haciendo un bucle a través de DataTable , obtengo el EventDate actual y lo compruebo con el anterior en.

Este es el código 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

Como no uso c# , el convertidor en línea convierte el código siguiente (espero que sea la conversión correcta :():

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;

Es un código poco sucio.

Actualizar (rellenar Repeater ):

Luego tiene que crear una nueva, vacía, DataTable y usar, casi, el mismo código, pero, en lugar de poner valores en una string , póngalos, por separado, en una nueva DataTable (con 2 columnas, una para fecha y otra para Títulos):

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# ) (ps No estoy seguro de que sea la conversión correcta. Lo siento, de nuevo, estoy muy, muy débil con 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;}

DataTable (ya ordenado por 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

Etiqueta ASP.net:

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

¿Cómo puedo codificarlo para que la tabla de datos se muestre en la etiqueta de esta manera? (Puede que no sea así, pero de alguna manera puedo separarlos por fechas y mostrarlos en diferentes lugares):

3/23: Testing 1 Testing 2 3/24: Testing 9 3/29: Testing 5 Testing 6 Testing 4


en archivo .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>

Y configurar el DataSource repetidor:

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

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


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


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