Adding a breakline/new line

Camillia used Ask the Experts™
Please note that this a mobile app and not an ASP.Net

I want to contact rows of a datatable, then add them to a list view. I want each row to be on separate lines. I did below but rows still show up on one line break between them. How can I do this??

 foreach (DataRow dr in restockItems.Rows)
                            lvItems.Items.Add(new ListViewItem(dr["Lane"].ToString() + " ASN " + dr["ASN"].ToString() + Environment.NewLine));

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
You want individual rows of data to have "sub rows" inside them?

I've never thought of trying that. Have you tried enabling MultiLine? Maybe that's the missing link, since you've already embedded the newlines.

I doubt it though, I don't think you can do it. I searched around and every discussion I found concurs.
No,I have for example 10 rows. Each row has 4 columns. This is in the datatable.

I contact first row's 2 columns. I display the first row. Now i want a line break. Then display the second rows by contacting second row's columns.

so I want :  

Just a line break.

Let me try MultiLine. This is a ListView...not sure if it has multi line.

yeah,  i dont see multiline property for ListView
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

So you want the typical list of multiple columns, and each row in the list represents a row from the database, right?

Multiline isn't the issue. It's most likely that you've got the listview's View property set to something other than "Details". Do you even see the column headers? I'm guessing not. Don't add newlines. Furthermore, it looks to me that you're trying to make columns by assembling a string from multiple values. Instead, you need to add "subitems" to each listview item. Then set the value of each to its own property from the DB.
ok, I changed the View property to "Details".

You're correct, i am creating a string because i didnt know how to bind to the listview.

Now, how can I do the subitems?? I have a DATATABLE.
Great, you're almost there.

Check this discussion out:

Scroll down a bit. One thing to note is that the first subitem is the first, default column that's always displayed. It can be referenced as Subitems[0], or via the listitem's Text property. A bit confusing sometimes, but now you know. You then add subitems to the listviewitem and set their values as shown in the linked example.
thanks, let me see.
Not sure why the rows wont display in the listview. I changed the view property to Details. I see a blue bar on top of the listview. I have my datatable. I can loop thru it. I set debug and i see the listview having items. But not sure when the code is done running, i do see the values in the Listview...

 foreach (DataRow dr in restockItems.Rows) //this is a datatable
                            ListViewItem item = new ListViewItem();
                            //item.Text = item.SubItems[0].Text;//"Lane";
                            item.Text = dr["Lane"].ToString();

Did you add columns to the listview first?

Back up and take this one step at a time. You've got a few concepts going on here and should start simple to prove out (and isolate) each and then intergrate them.

This code definitely works:
ListViewItem item;

            item = listView1.Items.Add("1-1");

            item = listView1.Items.Add("2-1");

            item = listView1.Items.Add("3-1");

Open in new window

Ah, i missed adding the columns. For some reason,  I thought since the database has the columns, i dont need to do it with ListView.
Imagine the data table had 15 columns, but you only want to display two. Or you want to translate them and combine some into singles. Or, you don't want the column to be labeled "pkPerson", to match the database column name.

It's your job to set up the UI control as you see fit and populate it similarly.
Yes, thanks. I think I have it now:

lvItems.Columns.Add("Lane",40, HorizontalAlignment.Center);
                        lvItems.Columns.Add("ASN",40, HorizontalAlignment.Center);
                        lvItems.Columns.Add("PO", 40, HorizontalAlignment.Center);
                        lvItems.Columns.Add("Score", 40, HorizontalAlignment.Center);

Will test it some more. Thanks for sticking with this.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial