0

This is my first foray into C# WinForms, and I'm trying to create a ListView grid.

My code looks like this:

listView1.Columns.Add("Name", 100);
listView1.Columns.Add("Col2", 200);
listView1.Columns.Add("Col3", 300);
string[] arr = new string[3];
arr[0] = "product_1";
arr[1] = "100";
arr[2] = "10";
string[] arr2 = new string[3];
arr[0] = "product_2";
arr[1] = "200";
arr[2] = "20";
ListViewItem itm = new ListViewItem(arr);
ListViewItem itm2 = new ListViewItem(arr2);
listView1.Items.Add(itm);
istView1.Items.Add(itm2);

But the output looks like this:

Result

So a few questions here:

  • Why do I not see column names?
  • Why do I not see grid lines?
  • Why is only the last entry being displayed (product2)?
  • Why is there no information other than the string "product2" displayed, rather than the other column entries?

Any help is greatly appreciated!

4
  • you created arr2 but are still assigning to arr: arr[0] = "product_2". Commented Nov 5, 2016 at 20:04
  • Did you set the View=Details? Commented Nov 5, 2016 at 20:05
  • @GiladGreen - Crap, you are right. That takes care of that part. Commented Nov 5, 2016 at 20:05
  • @Taw - Aand that takes care of the rest of the others. Thank you! I feel like an idiot Commented Nov 5, 2016 at 20:07

3 Answers 3

1

The default viewstyle of a ListView is LargeIcon. In that view, no columns or details are displayed. If you want your columns to be visible, you should set the View property of the ListView to Details:

listView1.View = View.Details;

On a sidenote: when you're adding lots of ListViewItems, it might be better to use the AddRange method instead of adding each item seperatly. That will significantly improve performance. Also, make use of the BeginUpdate() and EndUpdate() methods on the ListView. This will prevent the ListView to be redrawn with each add of a ListViewItem.

Sign up to request clarification or add additional context in comments.

2 Comments

You are right. There were a lot of people who caught the bug in my stupid array assignment, but trickier one was the View property change. Unlike the stupid spelling bug, I would not have found that one on my own...and that's why I'm marking this answer correct, even though they all technically are. Thanks a lot!
Thanks for the tips. I will read and put them into practice.
0

You're creating new array and that assigning values to the old one in this fragment:

string[] arr2 = new string[3];
arr[0] = "product_2";  //arr instead of arr2 here!
arr[1] = "200";
arr[2] = "20";

It should be:

string[] arr2 = new string[3];
arr2[0] = "product_2";
arr2[1] = "200";
arr2[2] = "20";

1 Comment

Crap you are right. What a dumb mistake. The second part of the puzzle here is setting the View poperty to Details.
0

You created arr2 but are assigning it's values to arr by mistake as shown below:

string[] arr2 = new string[3];
arr[0] = "product_2"; // Should be arr2[0]
arr[1] = "200"; // Should be arr2[1]
arr[2] = "20"; // Should be arr2[2]

Replace your code with the following:

listView1.Columns.Add("Name", 100);
listView1.Columns.Add("Col2", 200);
listView1.Columns.Add("Col3", 300);

string[] arr = new string[3];
arr[0] = "product_1";
arr[1] = "100";
arr[2] = "10";

string[] arr2 = new string[3];
arr2[0] = "product_2";
arr2[1] = "200";
arr2[2] = "20";

ListViewItem itm = new ListViewItem(arr);
ListViewItem itm2 = new ListViewItem(arr2);

listView1.Items.Add(itm);
istView1.Items.Add(itm2);

If you want your column names to be visible, set the ListItem's View property to details:

listView1.View = View.Details;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.