m ali
m ali

Reputation: 646

get data to listview

I have made a DataTable in code behind, in VB.NET. I need to call it to from my .ASPX page and show this as a ListView.

My code behind is:

Dim table As New DataTable

' columns in the DataTable.
table.Columns.Add("Monday", System.Type.GetType("System.Int32"))
table.Columns.Add("Tuesday", System.Type.GetType("System.Int32"))
table.Columns.Add("Wednesday", System.Type.GetType("System.Int32"))
table.Columns.Add("Thursday", System.Type.GetType("System.Int32"))
table.Columns.Add("Friday", System.Type.GetType("System.Int32"))
'  rows with those columns filled in the DataTable.
table.Rows.Add(1, 2005, 2000, 4000, 34)
table.Rows.Add(2, 3024, 2343, 2342, 12)
table.Rows.Add(3, 2320, 9890, 1278, 2)
table.Rows.Add(4, 3420, 1234, 4321, 89)
table.Rows.Add(5, 3240, 6600, 1100, 56)
table.Rows.Add(6, 4320, 1100, 3243, 23)
table.Rows.Add(7, 4540, 7645, 4324, 56)

I want to only show Monday and Tuesday on the .ASPX page. How will i do this? At the moment I have this:

    <asp:ListView ID="Listview1" runat="server">
    <LayoutTemplate>
        <table>
            <tr>
                <th>
                </th>
                <th>
                    monday
                </th>
                <th>
                    tuesday
                </th>
                <th>
                    wednesday
                </th>
            </tr>
            <tr id="holder" runat="server">
            </tr>
        </table>
    </LayoutTemplate>
    <ItemTemplate>
        <tr>
            <!--some code goes here am guessing-->
        </tr>
    </ItemTemplate>
</asp:ListView>

this in the aspx code behind?

Property readonly Data() As DataTable



End Property

Upvotes: 0

Views: 986

Answers (1)

Mez
Mez

Reputation: 4726

In your ASPX the following:

 <asp:ListView ID="table" runat="server" ItemPlaceholderID="itemPlaceholder">
        <LayoutTemplate>
            <table>
                <tr>
                    <th>
                    </th>
                    <th>
                        monday
                    </th>
                    <th>
                        tuesday
                    </th>
                </tr>
                <tr runat="server" id="itemPlaceholder" />
            </table>
        </LayoutTemplate>
        <ItemTemplate>
            <tr id="Tr1" runat="server">
                <td>
                    <asp:Label ID="lblmon" runat="Server" Text='<%#Eval("monday") %>' />
                </td>
                <td>
                    <asp:Label ID="lbltues" runat="Server" Text='<%#Eval("tuesday") %>' />
                </td>
            </tr>
        </ItemTemplate>
    </asp:ListView>

And in your vb.net the following:

 Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    Dim table As New DataTable

    ' columns in the DataTable.
    table.Columns.Add("Monday", System.Type.GetType("System.Int32"))
    table.Columns.Add("Tuesday", System.Type.GetType("System.Int32"))
    table.Columns.Add("Wednesday", System.Type.GetType("System.Int32"))
    table.Columns.Add("Thursday", System.Type.GetType("System.Int32"))
    table.Columns.Add("Friday", System.Type.GetType("System.Int32"))
    '  rows with those columns filled in the DataTable.
    table.Rows.Add(1, 2005, 2000, 4000, 34)
    table.Rows.Add(2, 3024, 2343, 2342, 12)
    table.Rows.Add(3, 2320, 9890, 1278, 2)
    table.Rows.Add(4, 3420, 1234, 4321, 89)
    table.Rows.Add(5, 3240, 6600, 1100, 56)
    table.Rows.Add(6, 4320, 1100, 3243, 23)
    table.Rows.Add(7, 4540, 7645, 4324, 56)

    Me.table.DataSource = table
    Me.table.DataBind()
End Sub

In an ASCX, put this code in a method, and call it in the Page_Load

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    BindData()
End Sub

Private Sub BindData()
    Dim table As New DataTable

    ' columns in the DataTable.
    table.Columns.Add("Monday", System.Type.GetType("System.Int32"))
    table.Columns.Add("Tuesday", System.Type.GetType("System.Int32"))
    table.Columns.Add("Wednesday", System.Type.GetType("System.Int32"))
    table.Columns.Add("Thursday", System.Type.GetType("System.Int32"))
    table.Columns.Add("Friday", System.Type.GetType("System.Int32"))
    '  rows with those columns filled in the DataTable.
    table.Rows.Add(1, 2005, 2000, 4000, 34)
    table.Rows.Add(2, 3024, 2343, 2342, 12)
    table.Rows.Add(3, 2320, 9890, 1278, 2)
    table.Rows.Add(4, 3420, 1234, 4321, 89)
    table.Rows.Add(5, 3240, 6600, 1100, 56)
    table.Rows.Add(6, 4320, 1100, 3243, 23)
    table.Rows.Add(7, 4540, 7645, 4324, 56)

    Me.table.DataSource = table
    Me.table.DataBind()
End Sub

Upvotes: 1

Related Questions