Print Datatable on a DIV using Bind or Eval

Asked

Viewed 376 times

-1

It is possible to print the data of a DataTable (codebehind) in a div?

Print the data from datatable in the Label.

How to use bind or eval? Or some other way?

  • Is there anything to stop you from using Gridview or Repeater ?

1 answer

0

The ideal is that you use a Gridview, Repeater or a Listview (these last two seem to be better for what you want, since you can mount them inside a div as you want). This link explains how to use Repeater: http://www.macoratti.net/aspn_rep.htm

Using Repeater:

Suppose your Datatable (dt) has the columns id, name, gender, dtNascimento. On your aspx page it would look something like this:

<div>
   <asp:Repeater ID="rptMeuRepeater" runat="server">
      <ItemTemplate>
         <%# DataBinder.Eval(Container.DataItem, "id")%><br />
         <%# DataBinder.Eval(Container.DataItem, "nome")%><br />
         <%# DataBinder.Eval(Container.DataItem, "sexo")%><br />
         <%# DataBinder.Eval(Container.DataItem, "dtNascimento")%>
      </ItemTemplate>
   </asp:Repeater>
</div>

And to popular this Peater, do in your codebehind:

rptMeuRepeater.DataSource = dt;
rptMeuRepeater.DataBind();

And doing as you asked:

You can go through the columns of a Datatable and fill its label, it is more work; but assuming that the Label that is in your div has the id "lblText", it would be more or less like this (in c#):

Foreach (DataRow r in dt.Rows){
  lblTexto.Text += r["id"] + "<br />";
  lblTexto.Text += r["nome"] + "<br />";
  lblTexto.Text += r["sexo"] + "<br />";
  lblTexto.Text += r["dtNascimento"] + "<br />";
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.