Link to home
Start Free TrialLog in
Avatar of Camillia
CamilliaFlag for United States of America

asked on

Adding a breakline/new line

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 row..no 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));

                        }
Avatar of ToddBeaulieu
ToddBeaulieu
Flag of United States of America image

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.
Avatar of Camillia

ASKER

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 :  
Row1
row2
row3

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
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.
ASKER CERTIFIED SOLUTION
Avatar of ToddBeaulieu
ToddBeaulieu
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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();
                            item.SubItems.Add(dr["ASN"].ToString());
                            item.SubItems.Add(dr["PO"].ToString());
                            item.SubItems.Add(dr["Score"].ToString());
                            lvItems.Items.Add(item);

                        }
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.SubItems.Add("1-2");
            item.SubItems.Add("1-3");

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

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

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.