do while loop in a table grid

What am I doing wrong here? I'm trying to get the table rows to repeat, but it's not working. Can anyone help?

<%@ Import Namespace="System.Data" %>
<%@ Page Language="VB" %>

<HTML>
  <HEAD>
<script language="vb" runat="server">

 Sub Page_Load()
     MyGrid.DataSource = CreateData()
     MyGrid.DataBind()
  End Sub
  Function CreateData() As ICollection
    Dim dt As New DataTable()
    Dim dr As DataRow
    Dim i as Integer = 1

      dt.Columns.Add(New DataColumn("Display List", GetType(Integer)))
        
       Do While i <= 5
            'Add a row
            dr = dt.NewRow()            
            dr(0)= i
            i = i + 1
        Loop
   

     Dim dv As New DataView(dt)
     Return dv
   End Function
   
</script>
</HEAD>
<body>

<p>Do While...Loop in a table I hope:</p><br><br>

<table>      
<tr>
 <td colspan="3">
                  <asp:label
                  id="title"
                  text="Amortization Table"
                  runat="server">
                  </asp:label>
                  
                  <asp:datagrid
                  id="MyGrid"
                  autogeneratecolumns="True"
                  runat="server">
                  </asp:datagrid>
            </td>
            </tr>
</table>

<p>This is the do loop not in the table </p>
<%
Dim i as Integer = 1
Do While i <= 5
%>
   <font size="<%=i%>">Welcome to ASP.NET</font><br>
<%
   i = i + 1
Loop
%>


</body>
</HTML>

Any help will be appreciated.

thanks

nflowers1228Asked:
Who is Participating?
 
Sam_JayanderConnect With a Mentor Commented:
Hi,

The problem is, you have created  a datarow-dr(which belongs to datatable-dt) and also assigned the value. But you have not added it to the datatable.

so insert this line in your do loop.

Do While i <= 5
          'Add a row
          dr = dt.NewRow()            
          dr(0)= i
          dt.Rows.Add(dr)     ----------> new line to be added.
          i = i + 1
Loop

Hope this will solve your problem.

Regards,
--Sam.
0
 
RejojohnyCommented:
why don't u use a datagrid and bind it to ur datasoure?. .. there is no need for all those loops and just requires 2-3 lines of code

Rejo
0
 
nflowers1228Author Commented:
Thanks Sam_Jayander! that seems to be doing the trick!
0
 
RejojohnyCommented:
am sorry .. should read the question more carefully :-( .. seems like u were indeed using a datagrid and just building a datatable from scrath ..
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.