List view new line for new values
The following code produces a ListView
with the names of the custom开发者_JS百科ers:
private void displayDeliveries()
{
lstDeliveryDetails.Items.Clear();
foreach (Delivery d in mainForm.myDeliveries)
{
lstDeliveryDetails.Items.Add(d.DeliveryName);
}
}
If I add, (d.DeliveryAddress), how can I get it to line up alongside the correct name?
I assume you mean that you want the delivery address to appear in the next column.
- Set ListView's View property to Details.
- Add two columns to the ListView's Collumns collection
- Use the following code to populate the ListView
private void displayDeliveries()
{
lstDeliveryDetails.Items.Clear();
foreach (Delivery d in mainForm.myDeliveries)
{
ListViewItem item = lstDeliveryDetails.Items.Add(d.DeliveryName);
item.SubItems.Add(d.DeliveryAddress);
}
}
Assuming want to have the Address followed by the name in each list item:
foreach (Delivery d in mainForm.myDeliveries)
{
lstDeliveryDetails.Items.Add(d.DeliveryName + " " + d.DeliveryAddress);
}
This simply concatenates the DeliveryName with a space (" "
) and then the DeliveryAddress strings.
Change the code in the for loop to:
ListViewItem item = lstDeliveryDetails.Items.Add(d.DeliveryName);
item.SubItems.Add(d.DeliveryAddress);
精彩评论