ListViewItem item = new ListViewItem(string)

patrick

Well-known member
Joined
Dec 5, 2021
Messages
238
Programming Experience
1-3
Hello

Please give me a Hint.
I am migrating code from ListViewItem item = new ListViewItem("Apple"); to ListViewDataItem item = new ListViewDataItem(Apple);.

However, code ListViewDataItem is not being migrated.

The end goal is list.Items.Add(item); to be.

I am trying so far to try to solve the problem.

However, it is not resolved.

Please give me a Hint.



C#:
ListViewItem item = new ListViewItem("Apple");
item.SubItems.Add("Banana");
                
list.Items.Add(item);



C#:
<asp:ListView ID="list" runat="server" Visible ="true">
<LayoutTemplate>
<table>
<tr>
<th>Apple</th>
<th>Banana</th>                                                                  
</tr>                                                                
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="ORDER_ID" Text='<%#Eval("ORDER_ID") %>' runat="server"></asp:Label></td>
<td>
<asp:Label ID="ORDER_ID1" Text='<%#Eval("ORDER_ID1") %>' runat="server"></asp:Label></td>
</tr>
</ItemTemplate>
</asp:ListView>
 
Last edited by a moderator:
"It's is not Insert to ListView."
"Please Help Me."
C#:
                      <asp:ListView ID="listAssign" runat="server" Visible="true">
                                                            <LayoutTemplate>
                                                                <table border="0px" cellpadding="1" width="100" >
                                                                    <tr>
                                                                        <th>ORDER_ID</th>
                                                                        <th>ORDER_TIME</th>
                                                                       </tr>
                                                                </table>
                                                            </LayoutTemplate>
                                                            <ItemTemplate>
                                                                <tr>
                                                                    <td>
                                                                        <asp:Label ID="ORDER_ID" Text='<%#Eval("ORDER_ID") %>' runat="server" Visible="true" Width ="100px"></asp:Label></td>
                                                                    <td>
                                                                        <asp:Label ID="PLAN_TIME" Text='<%#Eval("ORDER_TIME") %>' runat="server" Visible="true" Width ="100px"></asp:Label></td>
                                                                </tr>
                                                            </ItemTemplate>
                                                        </asp:ListView>


C#:
  public struct AssignInfo
        {
            public string ORDER_ID;
            public string ORDER_TIME;
        }
                        List<AssignInfo> listUsers = new List<AssignInfo>();
                        AssignInfo tmp = new AssignInfo();
                        tmp.ORDER_ID = "1";
                        tmp.ORDER_TIME = "20210114";
                        listUsers.Add(tmp);

                        ListViewDataItem newItem = new ListViewDataItem(listAssign.Items.Count, listAssign.Items.Count);
                        newItem.DataItem = listUsers;
                        this.listAssign.Items.Insert(listAssign.Items.Count, newItem);
                        this.listAssign.Items.Add(newItem);
 
Last edited by a moderator:
Thank you for putting in code as text instead of screenshots. Next step is to remember to put the code in code tags. It's the button on the toolbar that looks like </>.
 
Three issues:
1) The ListViewDataItem constructor takes indexes. Recall that in C# indexes are zero based.
2) Why are you using both Insert() and Add() ?
3) Where are you calling DataBind()?

And actually there is a fourth issue, you really shouldn't have to create ListViewDataItems and putting them into the list view yourself. Just let the data binding do this for you.
 
Last edited:
Merging in with your old thread since this is essentially the same topic.
 
I can't take watching the flailing anymore... Here's a simple .ASPX page that demonstrates populating a WebForms ListView using data from a List<T> of POCOs.
HTML:
<%@ Page Title="Test ListView" Language="C#" AutoEventWireup="true" %>

<script runat="server">
    class Name
    {
        public string First { get; set; }
        public string Last { get; set; }
    }
</script>

<%
    var list = new List<Name>()
    {
        new Name() { First = "Bruce", Last = "Wayne" },
        new Name() { First = "Clark", Last = "Kent" }
    };

    listView.DataSource = list;
    listView.DataBind();
%>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>Test ListView</title>
</head>
<body>
  <form id="form1" runat="server">
    <div>
      <asp:ListView runat="server" id="listView">
        <LayoutTemplate>
          <table runat="server" id="namesTable">
            <tr runat="server">
              <th runat="server">First Name</th>
              <th runat="server">Last Name</th>
            </tr>
            <tr runat="server" id="itemPlaceholder" />
          </table>
        </LayoutTemplate>
        <ItemTemplate>
          <tr runat="server">
            <td>
              <asp:Label runat="server" id="firstNameLabel" Text='<%# Eval("First") %>' />
            </td>
            <td>
              <asp:Label runat="server" id="lastNameLabel" Text='<%# Eval("Last") %>' />
            </td>
          </tr>
        </ItemTemplate>
      </asp:ListView>
    </div>
  </form>
</body>
</html>

which results in something like this:
Screenshot_1.png
 
I can't take watching the flailing anymore... Here's a simple .ASPX page that demonstrates populating a WebForms ListView using data from a List<T> of POCOs.
HTML:
<%@ Page Title="Test ListView" Language="C#" AutoEventWireup="true" %>

<script runat="server">
    class Name
    {
        public string First { get; set; }
        public string Last { get; set; }
    }
</script>

<%
    var list = new List<Name>()
    {
        new Name() { First = "Bruce", Last = "Wayne" },
        new Name() { First = "Clark", Last = "Kent" }
    };

    listView.DataSource = list;
    listView.DataBind();
%>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>Test ListView</title>
</head>
<body>
  <form id="form1" runat="server">
    <div>
      <asp:ListView runat="server" id="listView">
        <LayoutTemplate>
          <table runat="server" id="namesTable">
            <tr runat="server">
              <th runat="server">First Name</th>
              <th runat="server">Last Name</th>
            </tr>
            <tr runat="server" id="itemPlaceholder" />
          </table>
        </LayoutTemplate>
        <ItemTemplate>
          <tr runat="server">
            <td>
              <asp:Label runat="server" id="firstNameLabel" Text='<%# Eval("First") %>' />
            </td>
            <td>
              <asp:Label runat="server" id="lastNameLabel" Text='<%# Eval("Last") %>' />
            </td>
          </tr>
        </ItemTemplate>
      </asp:ListView>
    </div>
  </form>
</body>
</html>

which results in something like this:
View attachment 1979


C#:
                        ListViewDataItem newItem = new ListViewDataItem(listAssign.Items.Count, listAssign.Items.Count);

                        newItem.DataItem = listUsers;

                        this.listAssign.Items.Insert(listAssign.Items.Count, newItem);

                        this.listAssign.Items.Add(newItem);
 
I can't take watching the flailing anymore... Here's a simple .ASPX page that demonstrates populating a WebForms ListView using data from a List<T> of POCOs.
HTML:
<%@ Page Title="Test ListView" Language="C#" AutoEventWireup="true" %>

<script runat="server">
    class Name
    {
        public string First { get; set; }
        public string Last { get; set; }
    }
</script>

<%
    var list = new List<Name>()
    {
        new Name() { First = "Bruce", Last = "Wayne" },
        new Name() { First = "Clark", Last = "Kent" }
    };

    listView.DataSource = list;
    listView.DataBind();
%>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>Test ListView</title>
</head>
<body>
  <form id="form1" runat="server">
    <div>
      <asp:ListView runat="server" id="listView">
        <LayoutTemplate>
          <table runat="server" id="namesTable">
            <tr runat="server">
              <th runat="server">First Name</th>
              <th runat="server">Last Name</th>
            </tr>
            <tr runat="server" id="itemPlaceholder" />
          </table>
        </LayoutTemplate>
        <ItemTemplate>
          <tr runat="server">
            <td>
              <asp:Label runat="server" id="firstNameLabel" Text='<%# Eval("First") %>' />
            </td>
            <td>
              <asp:Label runat="server" id="lastNameLabel" Text='<%# Eval("Last") %>' />
            </td>
          </tr>
        </ItemTemplate>
      </asp:ListView>
    </div>
  </form>
</body>
</html>

which results in something like this:
View attachment 1979

ViewState["NameList"] = list; ====> Serialize Error
How do we solve Serialize Error?
 
Why do you need to serialize the list into the view state? Your original objective was to show a static list of items into a list view. Also, the list view carries around it's own view state so there's is no need to rebind on each post back.
 
Back
Top Bottom