9

I have a list of objects. I want to add these items to a ListView. I'm trying to add each list item row wise but format is very bad, it should be in proper table type format.

 List<string> lst = new List<string>(); lst.Add("John dsfsfsdfs " + "1" + 100); lst.Add("Smith sdfsdfsdfs" + "2" + 120); lst.Add("Cait dsffffffffffffffffffffff" + "3" + 97); lst.Add("Irene" + "4" + 100); lst.Add("Ben" + "5" + 100); lst.Add("Deniel jjhkh " + "6" + 88); foreach(string pl in lst) { listView1.Items.Add(pl); } 

Items are not visible and it should be in proper format.

Table Format

4
  • 2
    The ListView control can display items in several ways. For tabular view make sure you set the View property to Details Commented May 8, 2017 at 7:31
  • i tried to set Details, but its was displaying blank listview Commented May 8, 2017 at 7:32
  • 3
    Have you set the Columns property? “If your ListView control does not have any column headers specified and you set the View property to Details, the ListView control will not display any items.” Commented May 8, 2017 at 8:06
  • 1
    not working is not a useful message! Commented May 8, 2017 at 11:46

3 Answers 3

24

To make it multicolumn:

1) set the ListView into Details mode:

 listView1.View = View.Details; 

2)set up your three columns:

 listView1.Columns.Add("Column1Name"); listView1.Columns.Add("Column2Name"); listView1.Columns.Add("Column3Name"); 

3) add your items:

 listView1.Items.Add(new ListViewItem(new string[]{"John dsfsfsdfs ", "1" , "100"})); 

4) To make it more viewable try:

listView1.GridLines = true; 

5) To hide columns headers:

 listView1.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None; 
Sign up to request clarification or add additional context in comments.

3 Comments

It's correct if you just want to add list of strings o one-column listView :) In your case you shuld avoid it. Just use everything below "To make it multicolumn" and it will be fine :)
is there any way to hide column name ??
And it really does not work. You were right. I'll correct my answer, sorry
6

You can use ListViewItem directly to create an item with subitems (-> "tabular") instead of just adding strings to your ListView:

listView1.Items.Add(new ListViewItem (new[]{"John dsfsfsdfs", "1", "100"})); listView1.Items.Add(new ListViewItem (new[]{"Smith sdfsdfsdfs", "2", "120"})); listView1.Items.Add(new ListViewItem (new[]{"Cait dsffffffffffffffffffffff", "3", "97"})); listView1.Items.Add(new ListViewItem (new[]{"Irene", "4", "100"})); listView1.Items.Add(new ListViewItem (new[]{"Ben", "5", "100"})); listView1.Items.Add(new ListViewItem (new[]{"Deniel jjhkh", "6", "88"})); 

1 Comment

Thanks very much, exactly what I was looking for, straight and simple.
1

can you create a DataTable and bind it as a source

foreach (DataRow row in data.Rows) { ListViewItem item = new ListViewItem(row[0].ToString()); for (int i = 1; i < data.Columns.Count; i++) { item.SubItems.Add(row[i].ToString()); } listView_Services.Items.Add(item); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.