locked
C# application using VS 2015 - Attempting to create a table for a listview RRS feed

  • Question

  • User-1999980185 posted

    I am attempting to create a table for a listview in C#.   I need sample code to create a table for a listview in the C# language.  The following code work in VB.  I need the to learn how to do the same in a C#.  Please send sample code.

    Here I create the table in VB
    ' Add Columns
            OutstandingTable.Columns.AddRange(New DataColumn() {New DataColumn("ddate", GetType(String)),
                New DataColumn("ddesc", GetType(String)),
                New DataColumn("ddebit", GetType(String)),
                New DataColumn("dincome", GetType(String)),
                New DataColumn("dmeth", GetType(String)),
                New DataColumn("dcate", GetType(String)),
                New DataColumn("dstatus", GetType(String)),
                New DataColumn("dtype", GetType(String)),
                New DataColumn("dcomm", GetType(String))})
    
    Here I add to a listview in VB
    
    Private Sub Add_My_Entry()
    
    
    
            OutstandingTable.Rows.Add(ddate, ddesc, ddebit, dincome, dmeth, dcate, dstatus, dtype, dcomm)
    
            ListView_Budget.DataSource = OutstandingTable
            ListView_Budget.DataBind()
    
        End Sub

    Friday, July 13, 2018 6:37 AM

All replies

  • User-821857111 posted

    Is this a Windows Forms application or a Web application?

    Friday, July 13, 2018 7:10 PM
  • User-1999980185 posted

    A Web Application.

    Sunday, July 15, 2018 4:24 AM
  • User-821857111 posted
    OutstandingTable.Columns.AddRange(new DataColumn[]{
    			new DataColumn("ddate", typeof(String)),
    			new DataColumn("ddesc", typeof(String)),
    			new DataColumn("ddebit", typeof(String)),
    			new DataColumn("dincome", typeof(String)),
    			new DataColumn("dmeth", typeof(String)),
    			new DataColumn("dcate", typeof(String)),
    			new DataColumn("dstatus", typeof(String)),
    			new DataColumn("dtype", typeof(String)),
    			new DataColumn("dcomm", typeof(String))
    			});
    
    void Add_My_Entry()
    {
        OutstandingTable.Rows.Add(ddate, ddesc, ddebit, dincome, dmeth, dcate, dstatus, dtype, dcomm);
        ListView_Budget.DataSource = OutstandingTable;
        ListView_Budget.DataBind();
    }

    Monday, July 16, 2018 7:08 AM
  • User-330142929 posted

    Hi Db2tech,

    According to your description, I have made a demo,wish it is useful to you.

    aspx.

           <div>
                <asp:ListView ID="ListView1" runat="server">
                    <ItemTemplate>
                            <tr>
                                <td><asp:Label ID="Label1" runat="server" Text='<%#Eval("Id") %>'></asp:Label></td>
                                <td>
                                    <asp:Label ID="Label2" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
                                </td>
                                <td>
                                    <asp:Label ID="Label3" runat="server" Text='<%# Eval("Price") %>'></asp:Label>
                                </td>
                            </tr>
                    </ItemTemplate>
                    <LayoutTemplate>
                        <table border="1" cellpadding="1" cellspacing="0">
                            <tr>
                                <th>Id</th>
                                <th>Name</th>
                                <th>Price</th>
                            </tr>
                            <tr id="ItemPlaceholder" runat="server">
                            </tr>
                        </table>
                    </LayoutTemplate>
                </asp:ListView>
            </div>

    Code behind.

            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    DataTable dt = GetDataTable();
                    dt.Rows.Add(1, "Apple", 12);
                    dt.Rows.Add(2, "Grape", 21);
                    dt.Rows.Add(3, "Pear", 23);
                    this.ListView1.DataSource = dt;
                    this.ListView1.DataBind();
                }
            }
            public DataTable GetDataTable()
            {
                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Price") });
                return dt;           
            }

    How it works.

    Feel free to let me know if you have any questions.

    Best Regards

    Abraham

    Monday, July 16, 2018 7:57 AM