ASPX VB.NET网页Listview列并添加列表

时间:2012-03-10 05:48:58

标签: vb.net

我的.aspx页面列表中有一个ListView,所以:

<asp:ListView ID="amortList" runat="server" Width="500px" Height="250px"></asp:ListView>

在我的.aspx.vb文件中,我有一些代码可以将总数放入每个列的数组中,如下所示:

    Dim cols(4) As String
    Dim itms As ListViewItemType
    For i As Integer = 1 To term 'start for loop for payments caculations

                    'runs through for loop and display results in listview
                    i2 = mort * interest
                    p = sum - i2
                    mort = mort - p
                    cols(0) = i
                    cols(1) = String.Format("$ {0:0,0.00}", i2).ToString
                    cols(2) = String.Format("$ {0:0,0.00}", p).ToString
                    cols(3) = String.Format("$ {0:0,0.00}", mort).ToString
                    itms = New ListViewItem(cols) 'error 2 happens here
                    amortList.Items.Add(itms) 'error 3 happens here

     Next


Error   2   Value of type '1-dimensional array of String' cannot be converted to 'System.Web.UI.WebControls.ListViewItemType'.  z:\Documents\Brandon\Visual Studio 2010\WebSites\WebSite3\Default.aspx.vb   53  45  z:\...\WebSite3\


Error   3   Value of type 'System.Web.UI.WebControls.ListViewItemType' cannot be converted to 'System.Web.UI.WebControls.ListViewDataItem'. z:\Documents\Brandon\Visual Studio 2010\WebSites\WebSite3\Default.aspx.vb   54  41  z:\...\WebSite3\

如果您想知道错误1错误与其他两个错误无关,那就是格式化错误。

我不确定我在这一点上做错了什么,我曾尝试谷歌但没有运气。基本上我在ListView中需要4列,每行都能够添加数据。

请帮助我这项工作约4个小时。

2 个答案:

答案 0 :(得分:0)

您可以将表格的TR标签放在ItemTemplate的开头和结尾,并可以将标签包装在TD标签中。

<强>标记:

    <asp:ListView ID="amortList" runat="server">
    <ItemTemplate>
        <asp:Label runat="server" Text='<%# Eval("[0]") %>'></asp:Label>
        <asp:Label runat="server" Text='<%# Eval("[1]") %>'></asp:Label>
        <asp:Label runat="server" Text='<%# Eval("[2]") %>'></asp:Label>
        <asp:Label runat="server" Text='<%# Eval("[3]") %>'></asp:Label>
    </ItemTemplate>
    <ItemSeparatorTemplate>
        <br />
    </ItemSeparatorTemplate>
    </asp:ListView>

<强>代码隐藏:

    For i As Integer = 1 To term
        i2 = mort * interest
        p = sum - i2
        mort = mort - p
        Dim cols(3) As String
        cols(0) = i
        cols(1) = String.Format("$ {0:0,0.00}", i2).ToString
        cols(2) = String.Format("$ {0:0,0.00}", p).ToString
        cols(3) = String.Format("$ {0:0,0.00}", mort).ToString
        lstCols.Add(cols)
    Next
    amortList.DataSource = lstCols
    amortList.DataBind()        

答案 1 :(得分:0)

我最终使用表格来显示我的信息。这不是我想要的方式,但它确实完成了工作。

                Dim rw As New TableRow()
                Dim cellNum As Integer
                For cellNum = 0 To 3 Step 1
                    Dim cel As New TableCell()
                    cel.Text = itm(cellNum)
                    rw.Cells.Add(cel)

                Next cellNum
                Me.amortTable.Rows.Add(rw)