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.
string[] columns = new string[] { i.Attributes["name"].ToString(), role.Name, i.Attributes["address1_city"].ToString(), i.Attributes["address1_postalcode"].ToString() };
ListViewItem item = new ListViewItem(columns);
this.CompanyList1.Items.Add(item);
Full Code:
public void PopulateListView(DataCollection<Entity> 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") && i.Attributes.Contains("address1_city") && i.Attributes.Contains("address1_postalcode"))
{
EntityReference role = (EntityReference)i.Attributes["new_primaryroleid"];
string[] columns = new string[] { i.Attributes["name"].ToString(), role.Name, i.Attributes["address1_city"].ToString(), i.Attributes["address1_postalcode"].ToString() };
ListViewItem item = new ListViewItem(columns);
this.CompanyList1.Items.Add(item);
}
if (i.Attributes.Contains("new_primaryroleid") && !i.Attributes.Contains("address1_city") && !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") && !i.Attributes.Contains("address1_city") && !i.Attributes.Contains("address1_postalcode"))
{
string[] columns = new string[] { i.Attributes["name"].ToString() };
ListViewItem item = new ListViewItem(columns);
this.CompanyList1.Items.Add(item);
}
}
}
Marked as answer by Sage Gu - MSFT on Jun 21, 2012 05:03 AM
ginger_asp
Member
255 Points
329 Posts
Re: How to put data in ListViewcolumns
Jun 20, 2012 12:23 PM|LINK
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.
string[] columns = new string[] { i.Attributes["name"].ToString(), role.Name, i.Attributes["address1_city"].ToString(), i.Attributes["address1_postalcode"].ToString() }; ListViewItem item = new ListViewItem(columns); this.CompanyList1.Items.Add(item);Full Code:
public void PopulateListView(DataCollection<Entity> 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") && i.Attributes.Contains("address1_city") && i.Attributes.Contains("address1_postalcode")) { EntityReference role = (EntityReference)i.Attributes["new_primaryroleid"]; string[] columns = new string[] { i.Attributes["name"].ToString(), role.Name, i.Attributes["address1_city"].ToString(), i.Attributes["address1_postalcode"].ToString() }; ListViewItem item = new ListViewItem(columns); this.CompanyList1.Items.Add(item); } if (i.Attributes.Contains("new_primaryroleid") && !i.Attributes.Contains("address1_city") && !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") && !i.Attributes.Contains("address1_city") && !i.Attributes.Contains("address1_postalcode")) { string[] columns = new string[] { i.Attributes["name"].ToString() }; ListViewItem item = new ListViewItem(columns); this.CompanyList1.Items.Add(item); } } }