How to put data in ListViewcolumnshttp://forums.asp.net/t/1807467.aspx/1?How+to+put+data+in+ListViewcolumnsWed, 20 Jun 2012 12:23:36 -040018074674997340http://forums.asp.net/p/1807467/4997340.aspx/1?How+to+put+data+in+ListViewcolumnsHow to put data in ListViewcolumns <p>At the moment the ListView gets my data - however, I want to show the data in both columns. Right now it simply displays both column values vertically.</p> <pre class="prettyprint">public void PopulateListView(DataCollection&lt;Entity&gt; items) { CompanyList1.Width = 450; CompanyList1.Height = 100; CompanyList1.Location = new System.Drawing.Point(10, 10); ColumnHeader header1, header2; header1 = new ColumnHeader(); header2 = new ColumnHeader(); header1.Text = &quot;Company Name&quot;; header1.TextAlign = HorizontalAlignment.Left; header1.Width = 70; header2.TextAlign = HorizontalAlignment.Left; header2.Text = &quot;Primary Role&quot;; header2.Width = 200; CompanyList1.Columns.Add(header1); CompanyList1.Columns.Add(header2); CompanyList1.View = View.Details; foreach (Entity i in items) { if (i.Attributes.Contains(&quot;new_primaryroleid&quot;)) { EntityReference role = (EntityReference)i.Attributes[&quot;new_primaryroleid&quot;]; CompanyList1.Items.Add(i.Attributes[&quot;name&quot;].ToString()); CompanyList1.Items.Add(role.Name); } else { CompanyList.Items.Add(i.Attributes[&quot;name&quot;]); } } }</pre> <p><br> <br> </p> 2012-05-25T11:28:33-04:005005906http://forums.asp.net/p/1807467/5005906.aspx/1?Re+How+to+put+data+in+ListViewcolumnsRe: How to put data in ListViewcolumns <p>Hi ginger_asp,</p> <p>Did you figure it out? If you solved this issue,&nbsp;you can share your solutions &amp; experience here, it will be very beneficial for other community members who have similar questions.</p> <p>If you don't resolve this issue, you can still&nbsp;post&nbsp;your&nbsp;consideration here.&nbsp;It would be really appreciated if you could provide us with any detailed information about what the way of showing column is.</p> <p>Regards,</p> <p>Sage Gu - MSFT</p> 2012-05-31T12:28:10-04:005033774http://forums.asp.net/p/1807467/5033774.aspx/1?Re+How+to+put+data+in+ListViewcolumnsRe: How to put data in ListViewcolumns <p>Sorry for the late reply. Went away for few weeks. Right, I did manage to get it to work at the end. The key was to store retrieved attributes in an array and then add the array to listviewitem then add to control.</p> <pre class="prettyprint">string[] columns = new string[] { i.Attributes[&quot;name&quot;].ToString(), role.Name, i.Attributes[&quot;address1_city&quot;].ToString(), i.Attributes[&quot;address1_postalcode&quot;].ToString() }; ListViewItem item = new ListViewItem(columns); this.CompanyList1.Items.Add(item);</pre> <p>Full Code:</p> <pre class="prettyprint"> public void PopulateListView(DataCollection&lt;Entity&gt; items) { #region Set ListView ColumnHeader header1, header2, header3, header4; header1 = new ColumnHeader(); header2 = new ColumnHeader(); header3 = new ColumnHeader(); header4 = new ColumnHeader(); header1.Text = "Company Name"; header1.TextAlign = HorizontalAlignment.Left; header1.Width = 100; header2.TextAlign = HorizontalAlignment.Left; header2.Text = "Primary Role"; header2.Width = 100; header3.Text = "City"; header3.TextAlign = HorizontalAlignment.Left; header3.Width = 100; header4.TextAlign = HorizontalAlignment.Left; header4.Text = "Postal Code"; header4.Width = 100; CompanyList1.Columns.Add(header1); CompanyList1.Columns.Add(header2); CompanyList1.Columns.Add(header3); CompanyList1.Columns.Add(header4); CompanyList1.View = View.Details; #endregion foreach (Entity i in items) { if (i.Attributes.Contains("new_primaryroleid") &amp;&amp; i.Attributes.Contains("address1_city") &amp;&amp; i.Attributes.Contains("address1_postalcode")) { EntityReference role = (EntityReference)i.Attributes["new_primaryroleid"]; string[] columns = new string[] { i.Attributes[&quot;name&quot;].ToString(), role.Name, i.Attributes[&quot;address1_city&quot;].ToString(), i.Attributes[&quot;address1_postalcode&quot;].ToString() }; ListViewItem item = new ListViewItem(columns); this.CompanyList1.Items.Add(item); } if (i.Attributes.Contains("new_primaryroleid") &amp;&amp; !i.Attributes.Contains("address1_city") &amp;&amp; !i.Attributes.Contains("address1_postalcode")) { EntityReference role = (EntityReference)i.Attributes["new_primaryroleid"]; string[] columns = new string[] { i.Attributes["name"].ToString(), role.Name }; ListViewItem item = new ListViewItem(columns); this.CompanyList1.Items.Add(item); } if (!i.Attributes.Contains("new_primaryroleid") &amp;&amp; !i.Attributes.Contains("address1_city") &amp;&amp; !i.Attributes.Contains("address1_postalcode")) { string[] columns = new string[] { i.Attributes["name"].ToString() }; ListViewItem item = new ListViewItem(columns); this.CompanyList1.Items.Add(item); } } }</pre> <p><br> <br> </p> 2012-06-20T12:23:36-04:00