ListViewItem item = new ListViewItem(string)

yunhyejeong

Well-known member
Joined
Dec 5, 2021
Messages
106
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:

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
4,431
Location
Chesapeake, VA
Programming Experience
10+
RTFM:


Thr constructor only takes index values where the second index value is an index into your data.

I highly recommend abandoning this approach of doing a mechanical port of your WinForms code and stepping back to see what the end goal is. In my view, the goal is display a bunch of rows of data. If it's only for display and the list of rows is static, the simplest approach maybe just to brute force hardcoding a <table> since in the end that is what the ListView generates on the client side. If the list of rows is not static, then a simple for loop for generating the table is still a viable option. If you really need a ListView because the user needs to interact/select with items displayed, then compose a list of items and set data as the data source for the ListView. If you actually took time to read the documentation for the ListView there is nothing that says that the data source must be rows retrieved from a database.
 

yunhyejeong

Well-known member
Joined
Dec 5, 2021
Messages
106
Programming Experience
1-3
RTFM:


Thr constructor only takes index values where the second index value is an index into your data.

I highly recommend abandoning this approach of doing a mechanical port of your WinForms code and stepping back to see what the end goal is. In my view, the goal is display a bunch of rows of data. If it's only for display and the list of rows is static, the simplest approach maybe just to brute force hardcoding a <table> since in the end that is what the ListView generates on the client side. If the list of rows is not static, then a simple for loop for generating the table is still a viable option. If you really need a ListView because the user needs to interact/select with items displayed, then compose a list of items and set data as the data source for the ListView. If you actually took time to read the documentation for the ListView there is nothing that says that the data source must be rows retrieved from a database.


What did you mean by this? (Temporary Table, DataSource)

After creating a temporary table, it is imported into the DataSource of the ListView.

What did you mean by this?

C#:
            int rowIndex = 0;

            if (ViewState["CurrentTable"] != null)
            {
                DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
                DataRow drCurrentRow = null;
                if (dtCurrentTable.Rows.Count > 0)
                {
                    for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                    {
                        TextBox TextBoxName = (TextBox)listview1.Items[rowIndex].FindControl("txtFName");
 
                        drCurrentRow = dtCurrentTable.NewRow();

                        dtCurrentTable.Rows[i - 1]["FName"] = TextBoxName.Text;
                        rowIndex++;
                    }
                    dtCurrentTable.Rows.Add(drCurrentRow);
                    ViewState["CurrentTable"] = dtCurrentTable;

                    listview1.DataSource = dtCurrentTable;
                    listview1.DataBind();

                    TextBox txn = (TextBox)listview1.Items[rowIndex].FindControl("txtFName");
                    txn.Focus();
                }
 
Last edited:

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
4,431
Location
Chesapeake, VA
Programming Experience
10+
How did we go from "Apple" and "Banana" column names to "FName"?

But anyway, something like that. That code there looks broken. It only binds a data table to the list view if it already has a table stored in the page view state. How do you put the initial data table into the page view state? Also the list view maintains its own view state including keeping track of its data source. Why is that code there maintaining a different view state? Why is that code creating dtCurrentTable.Rows.Count rows, but only adding the last row it created? Why is that code changing thing "FName" column values?
 

yunhyejeong

Well-known member
Joined
Dec 5, 2021
Messages
106
Programming Experience
1-3
How did we go from "Apple" and "Banana" column names to "FName"?

But anyway, something like that. That code there looks broken. It only binds a data table to the list view if it already has a table stored in the page view state. How do you put the initial data table into the page view state? Also the list view maintains its own view state including keeping track of its data source. Why is that code there maintaining a different view state? Why is that code creating dtCurrentTable.Rows.Count rows, but only adding the last row it created? Why is that code changing thing "FName" column values?

The attached source is an example.

Do you mean to do it DataTable&DataSource instead of ListviewDataItem& Add(ListviewDataItem) ?

I am migrating code from ListViewItem item = new ListViewItem("Apple"); to ListViewDataItem item = new ListViewDataItem(Apple);.

I'm doing a simple mechanical code migration. ( C#Winform ListView Code ---> ASP.ET Webform ListView Code )

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:

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
4,431
Location
Chesapeake, VA
Programming Experience
10+
There is no need for you to deal with the ListViewDataItem. Just create a list of POCOs that represent each row. Bind that list to the ListView. The binding will create the ListViewDataItems.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
4,431
Location
Chesapeake, VA
Programming Experience
10+
I'm doing a simple mechanical code migration. ( C#Winform ListView Code ---> ASP.ET Webform ListView Code )
You cannot do that because the WinForms ListView concepts don't translate to the WebForms ListView. The WinForms ListView has an underlying Win32 API ListViewItem structure/object for each row. The WebForms ListView has the HTML <tr> to represent each row. There is no easy direct translation.
 

yunhyejeong

Well-known member
Joined
Dec 5, 2021
Messages
106
Programming Experience
1-3
You cannot do that because the WinForms ListView concepts don't translate to the WebForms ListView. The WinForms ListView has an underlying Win32 API ListViewItem structure/object for each row. The WebForms ListView has the HTML <tr> to represent each row. There is no easy direct translation.

insertitemtemplate ???
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
4,431
Location
Chesapeake, VA
Programming Experience
10+
*sigh* Re-read the link I posted in your old thread. Were they using the InsertItemTemplate just to display items?
 

yunhyejeong

Well-known member
Joined
Dec 5, 2021
Messages
106
Programming Experience
1-3
*sigh* Re-read the link I posted in your old thread. Were they using the InsertItemTemplate just to display items?

I still haven't been able to solve this problem.😭

My ideas are below. example

C#:
struct book{
    public string id;
}

book temp = new book();
List<book> list = new List<book>();
list.Add(temp);
list[0].id = "Apple";
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
4,431
Location
Chesapeake, VA
Programming Experience
10+
Okay. So what happens when you bind that to a ListView?
 

yunhyejeong

Well-known member
Joined
Dec 5, 2021
Messages
106
Programming Experience
1-3
Okay. So what happens when you bind that to a ListView?

I wrote the code like this, but I haven't run it yet.
My idea for the problem above is the List<class> structure in ListView


C#:
  public class stlistAssign
        {
            public string chkSELECT;
            public string ORDER_ID;
            public string ORDER_DATE;
            public string LINE_CODE;
            public string PLAN_TIME;
            public string PRODUCT_CODE;
            public string FLOW_CODE;
            public string ORDER_STATUS;
            public string ORDER_STATUS_NM;
            public string SEQ;
            public string ALC_CODE;
            public string BLOCK_STATUS;
        }

        public class stlistNoAssign
        {
            public string chkSELECT;
            public string ORDER_ID;
            public string ORDER_DATE;
            public string LINE_CODE;
            public string PLAN_TIME;
            public string PRODUCT_CODE;
            public string FLOW_CODE;
            public string ORDER_STATUS;
            public string ORDER_STATUS_NM;
            public string SEQ;
            public string ALC_CODE;
            public string BLOCK_STATUS;
        }     
                stlistAssign lvOriginal = new stlistAssign();
                List<stlistAssign> ltlvOriginal = new List<stlistAssign>();
                for (int i = 0; i < listAssign.Items.Count; i++)
                {
                        lvOriginal.chkSELECT = "";
                        lvOriginal.ORDER_ID = listAssign.Items[i].FindControl("ORDER_ID").ToString();
                        lvOriginal.ORDER_DATE = listAssign.Items[i].FindControl("ORDER_DATE").ToString();
                        lvOriginal.LINE_CODE = listAssign.Items[i].FindControl("LINE_CODE").ToString();
                        lvOriginal.PLAN_TIME = listAssign.Items[i].FindControl("PLAN_TIME").ToString();
                        lvOriginal.PRODUCT_CODE = listAssign.Items[i].FindControl("PRODUCT_CODE").ToString();
                        lvOriginal.FLOW_CODE = listAssign.Items[i].FindControl("FLOW_CODE").ToString();
                        lvOriginal.ORDER_STATUS = listAssign.Items[i].FindControl("ORDER_STATUS").ToString();
                        lvOriginal.ORDER_STATUS_NM = listAssign.Items[i].FindControl("ORDER_STATUS_NM").ToString();
                        lvOriginal.SEQ = listAssign.Items[i].FindControl("SEQ").ToString();
                        lvOriginal.ALC_CODE = listAssign.Items[i].FindControl("ALC_CODE").ToString();
                        lvOriginal.BLOCK_STATUS = listAssign.Items[i].FindControl("BLOCK_STATUS").ToString();

                        ltlvOriginal.Add(lvOriginal);
                 }


                stlistNoAssign stlistNoAssign = new stlistNoAssign();
                List<stlistNoAssign> lslistNoAssign = new List<stlistNoAssign>();

                // From Assign To NoAssign =================
                for (int i = 0; i < listAssign.Items.Count; i++)
                {
                    CheckBox chk = this.listAssign.Items[i].FindControl("chkSELECT") as CheckBox;
                    if (chk.Checked == true)
                    {
                        Label lbtmp1 = (Label)listAssign.Items[i].FindControl("ORDER_ID");
                        lbtmp1.BackColor = System.Drawing.Color.Yellow;                       
                        Label lbtmp2 = (Label)listAssign.Items[i].FindControl("PLAN_TIME");
                        lbtmp2.BackColor = System.Drawing.Color.Yellow;
                        Label lbtmp3 = (Label)listAssign.Items[i].FindControl("PRODUCT_CODE");
                        lbtmp3.BackColor = System.Drawing.Color.Yellow;
                        Label lbtmp4 = (Label)listAssign.Items[i].FindControl("ALC_CODE");
                        lbtmp4.BackColor = System.Drawing.Color.Yellow;
                        Label lbtmp5 = (Label)listAssign.Items[i].FindControl("FLOW_CODE");
                        lbtmp5.BackColor = System.Drawing.Color.Yellow;
                        Label lbtmp6 = (Label)listAssign.Items[i].FindControl("ORDER_STATUS");
                        lbtmp6.BackColor = System.Drawing.Color.Yellow;

                        stlistNoAssign.ORDER_ID = lbtmp1.Text;
                        stlistNoAssign.PLAN_TIME = lbtmp2.Text;
                        stlistNoAssign.PRODUCT_CODE = lbtmp3.Text;
                        stlistNoAssign.ALC_CODE = lbtmp4.Text;
                        stlistNoAssign.FLOW_CODE = lbtmp5.Text;
                        stlistNoAssign.ORDER_STATUS = lbtmp6.Text;

                        lslistNoAssign.Add(stlistNoAssign);
                    }
                }

                for (int i = 0; i < lslistNoAssign.Count; i++)
                {
                    Label lbtmp2 = (Label)this.listNoAssign.Items[i].FindControl("ORDER_ID");
                    lbtmp2.Text = lslistNoAssign[i].ORDER_ID;

                    Label lbtmp3 = (Label)this.listNoAssign.Items[i].FindControl("PLAN_TIME");
                    lbtmp3.Text = lslistNoAssign[i].PLAN_TIME;

                    Label lbtmp4 = (Label)this.listNoAssign.Items[i].FindControl("PRODUCT_CODE");
                    lbtmp4.Text = lslistNoAssign[i].PRODUCT_CODE;

                    Label lbtmp5 = (Label)this.listNoAssign.Items[i].FindControl("ALC_CODE");
                    lbtmp5.Text = lslistNoAssign[i].ALC_CODE;

                    Label lbtmp6 = (Label)this.listNoAssign.Items[i].FindControl("FLOW_CODE");
                    lbtmp6.Text = lslistNoAssign[i].FLOW_CODE;

                    Label lbtmp7 = (Label)this.listNoAssign.Items[i].FindControl("ORDER_STATUS");
                    lbtmp7.Text = lslistNoAssign[i].ORDER_STATUS;
                }
                //<==== From Assign To NoAssign =================


                //Assign Display Update==============
                for (int i = 0; i < ltlvOriginal.Count; i++)
                {
                    if (i < this.listAssign.Items.Count)
                    {
                        stlistAssign temp = ltlvOriginal[i];

                        Label lbtmp2 = (Label)listAssign.Items[i].FindControl("PRODUCT_CODE");
                        temp.PRODUCT_CODE = lbtmp2.Text;

                        Label lbtmp3 = (Label)listAssign.Items[i].FindControl("ALC_CODE");
                        temp.ALC_CODE = lbtmp3.Text;
  
                        Label lbtmp4 = (Label)listAssign.Items[i].FindControl("FLOW_CODE");
                        temp.FLOW_CODE = lbtmp4.Text;

                        Label lbtmp5 = (Label)listAssign.Items[i].FindControl("ORDER_STATUS_NM");
                        temp.ORDER_STATUS_NM = lbtmp5.Text;

                        Label lbtmp6 = (Label)listAssign.Items[i].FindControl("BLOCK_STATUS");
                        temp.BLOCK_STATUS = lbtmp6.Text;

                        ltlvOriginal[i] = temp;
                    }
                    else
                    {
                        stlistAssign temp = ltlvOriginal[i];
                        temp.PRODUCT_CODE = string.Empty;
                        temp.ALC_CODE = string.Empty;
                        temp.FLOW_CODE = string.Empty;
                        temp.ORDER_STATUS_NM = string.Empty;
                        temp.BLOCK_STATUS = string.Empty;
                        ltlvOriginal[i] = temp;
                    }                   
                }


                listAssign.Items.Clear();

                for (int i = 0; i < ltlvOriginal.Count; i++)
                {
                    Label lbAf2 = (Label)listAssign.Items[i].FindControl("ORDER_ID");
                    lbAf2.Text = ltlvOriginal[i].ORDER_ID;

                    Label lbAf3 = (Label)listAssign.Items[i].FindControl("PLAN_TIME");
                    lbAf3.Text = ltlvOriginal[i].PLAN_TIME;

                    Label lbAf4 = (Label)listAssign.Items[i].FindControl("PRODUCT_CODE");
                    lbAf4.Text = ltlvOriginal[i].PRODUCT_CODE;

                    Label lbAf5 = (Label)listAssign.Items[i].FindControl("ALC_CODE");
                    lbAf5.Text = ltlvOriginal[i].ALC_CODE;

                    Label lbAf6 = (Label)listAssign.Items[i].FindControl("FLOW_CODE");
                    lbAf6.Text = ltlvOriginal[i].FLOW_CODE;

                    Label lbAf7 = (Label)listAssign.Items[i].FindControl("ORDER_STATUS_NM");
                    lbAf7.Text = ltlvOriginal[i].ORDER_STATUS_NM;

                    Label lbAf8 = (Label)listAssign.Items[i].FindControl("BLOCK_STATUS");
                    lbAf8.Text = ltlvOriginal[i].BLOCK_STATUS;

                    Label lbAf9 = (Label)listAssign.Items[i].FindControl("SEQ");
                    lbAf9.Text = ltlvOriginal[i].SEQ;
                }

                ltlvOriginal.Clear();
                lslistNoAssign.Clear();
                //Assign Display Update==============
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
4,431
Location
Chesapeake, VA
Programming Experience
10+
I wrote the code like this, but I haven't run it yet
Poor idea. Write a small bit of code. Test it. If it works, commit or backup the code and then add a little bit more. Test. Lather, rinse, repeat.

Even better would be to do your learning on a small self contained project first. Do small steps and small changes making backups or commits along the way. Learn what you need to learn, then go back to the main project and then incrementally make changes, testing, and making backups or committing along the way.
 

yunhyejeong

Well-known member
Joined
Dec 5, 2021
Messages
106
Programming Experience
1-3
Poor idea. Write a small bit of code. Test it. If it works, commit or backup the code and then add a little bit more. Test. Lather, rinse, repeat.

Even better would be to do your learning on a small self contained project first. Do small steps and small changes making backups or commits along the way. Learn what you need to learn, then go back to the main project and then incrementally make changes, testing, and making backups or committing along the way.




I'll report the results when I'm done.

C#:
stlistAssign stlvOriginal = new stlistAssign();
List<stlistAssign> ltlvOriginal = new List<stlistAssign>();

The code above is my idea for the problem below.

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

yunhyejeong

Well-known member
Joined
Dec 5, 2021
Messages
106
Programming Experience
1-3
"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:

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
4,431
Location
Chesapeake, VA
Programming Experience
10+
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 </>.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
4,431
Location
Chesapeake, VA
Programming Experience
10+
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:

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
4,431
Location
Chesapeake, VA
Programming Experience
10+
Merging in with your old thread since this is essentially the same topic.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
4,431
Location
Chesapeake, VA
Programming Experience
10+
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
 
Top Bottom