简体   繁体   中英

C# How to get the subitem on a listview to stay on the same line as the item?

So I have the following code:

    listView1.Items.Add("Test");
                ListViewItem testing = new ListViewItem();
                testing.SubItems.Add("Test2");
                listView.Items.Add(testing);

And here's what it produces:


(source: gyazo.com )

Not sure why the "Test2" goes at the bottom.

First of all Both list view are same listView1 and listView? If yes then here is the Answer:

listView1.Items.Add("Test"); while wrting this line of code you are adding first item in List view with main item "Test" and all subitems empty.

listView.Items.Add(testing); After doing this you are adding second item in the list view with main item Empty and first subitem as "Test2"

To add one item in listView with subitem try this:

            ListViewItem testing = new ListViewItem(); 
            testing.Text="Test";
            testing.SubItems.Add("Test2");
            listView.Items.Add(testing);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM