Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to remove unwanted whitespace when filling listview with items (c#)
    primarykey
    data
    text
    <p>Using c#, winforms. </p> <p><strong>Background:</strong></p> <p>The user should be able to select items from my menu strip, and then based on that the list view columns should be populated. Example: Select percent from 100-80 % then columns 1 and 2 get populated. Select total trans from &lt;1000 and then column 3 and 4 get populated.</p> <p><strong>Problem:</strong></p> <p>When adding items to my listview, say I select option 1 from menu strip. Then columns 1 and 2 get filled up. Good. BUT right after if I select option from total trans, the column 3 and 4 do get filled up, BUT there is a ton of WHITEspace in the columns. Basically, the columns should not have white space when getting filled up.</p> <p>This is what I mean: Notice the whitespace (outlined by red). First I selected option 1, then as soon as I selecte the option to populate column 3 and 4, they filled, but with whitespace. The column values for col 3 and 4 should have nothing above them in white.</p> <p><img src="https://i.stack.imgur.com/nEcPQ.png" alt="enter image description here"></p> <p>Also here: I select another option from menu strip (for col 1 and 2) , <em>after selecting the option to fill column 3 and 4,</em> and more whitespace:</p> <p><img src="https://i.stack.imgur.com/ObnP9.png" alt="enter image description here"></p> <p><strong>Code:</strong></p> <pre><code> // Fill Column 1 and 2 from option 1 private void toolStripMenuItem2_Click(object sender, EventArgs e) { int totItems = Seq3.Count - 1; if (PercentPopTolerance1.Count - 1 &gt; totItems) totItems = PercentPopTolerance1.Count - 1; for (int i = 0; i &lt;= totItems; i++) { ListViewItem lvi = new ListViewItem(); string item1 = ""; string item2 = ""; if (Seq3.Count - 1 &gt;= i) item1 = Seq3[i].ToString(); if (PercentPopTolerance1.Count - 1 &gt;= i) item2 = PercentPopTolerance1[i].ToString(); lvi.SubItems.Add(item1); lvi.SubItems.Add(item2); listView2.Items.Add(lvi); } } // Percent tolerance from 80-60% // Fill Column 1 and 2 from option 2 private void toolStripMenuItem3_Click_1(object sender, EventArgs e) { ClearColumn("columnHeader5"); int totItems = Seq4.Count - 1; if (PercentPopTolerance2.Count - 1 &gt; totItems) totItems = PercentPopTolerance2.Count - 1; for (int i = 0; i &lt;= totItems; i++) { ListViewItem lvi = new ListViewItem(); string item1 = ""; string item2 = ""; if (Seq4.Count - 1 &gt;= i) item1 = Seq4[i].ToString(); if (PercentPopTolerance2.Count - 1 &gt;= i) item2 = PercentPopTolerance2[i].ToString(); lvi.SubItems.Add(item1); lvi.SubItems.Add(item2); listView2.Items.Add(lvi); } } // Fill columns 3 and 4 from option in total trans menustrip // Total trans tolerance &lt; 1000 private void etcToolStripMenuItem_Click(object sender, EventArgs e) { int totItems = YYMMt21.Count - 1; if (TotalTransIrregularitiest21.Count - 1 &gt; totItems) totItems = TotalTransIrregularitiest21.Count - 1; for (int i = 0; i &lt;= totItems; i++) { ListViewItem lvi = new ListViewItem(); string item1 = ""; string item2 = ""; if (YYMMt21.Count - 1 &gt;= i) item1 = YYMMt21[i].ToString(); if (TotalTransIrregularitiest21.Count - 1 &gt;= i) item2 = TotalTransIrregularitiest21[i].ToString(); // Skip first 2 columns lvi.SubItems.Add(string.Empty); lvi.SubItems.Add(string.Empty); lvi.SubItems.Add(item1); lvi.SubItems.Add(item2); listView2.Items.Add(lvi); } } </code></pre> <p><strong><em>EDIT: Trying User suggestion:</em></strong></p> <p>New problem arises: what is supposed to be in column 3 and 4 vertically now fills the listview from the first row horizontally: <img src="https://i.stack.imgur.com/4Jxfw.png" alt="enter image description here"></p> <p>My attempt:</p> <pre><code> private void etcToolStripMenuItem_Click(object sender, EventArgs e) { int totItems = YYMMt21.Count - 1; if (TotalTransIrregularitiest21.Count - 1 &gt; totItems) totItems = TotalTransIrregularitiest21.Count - 1; for (int i = 0; i &lt;= totItems; i++) { ListViewItem lvi = new ListViewItem(); string item1 = ""; string item2 = ""; if (YYMMt21.Count - 1 &gt;= i) item1 = YYMMt21[i].ToString(); if (TotalTransIrregularitiest21.Count - 1 &gt;= i) item2 = TotalTransIrregularitiest21[i].ToString(); // Skip first 2 columns lvi.SubItems.Add(string.Empty); lvi.SubItems.Add(string.Empty); int rowToPopulate = 0; int colToPopulate = 0; if (rowToPopulate &lt;= listView2.Items.Count - 1) { //Editing an existing row/ListViewItem listView2.Items[rowToPopulate].SubItems.Insert(colToPopulate, new ListViewItem.ListViewSubItem() { Text = item1 }); // How would I also add item2? // listView2.Items[rowToPopulate].SubItems.Insert(colToPopulate, new ListViewItem.ListViewSubItem() { Text = item2 }); } else { //Adding a new row/ListViewItem // ListViewItem lvi = new ListViewItem(); lvi.SubItems.Add(item1); lvi.SubItems.Add(item2); //Add all the other Subitems as usual listView2.Items.Add(lvi); } //listView2.Items.Add(lvi); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload