列表视图填写问题

时间:2009-10-13 02:56:57

标签: c# .net winforms listview

我需要知道这里发生了什么。实际上我的目标是另外添加一个列并显示内容。我添加了列但我想知道 完成显示该列中项目所需的代码流

ManagedDeviceCollection list = new ManagedDeviceCollection();
try
{
    if(SpoServer == null)
       return;

    _listSelected.BeginUpdate();
    _listAvailable.BeginUpdate();

    #region populate the selected list

    // Collect selected items.
    object[] selected = new object[_listSelected.SelectedItems.Count];
    _listSelected.SelectedItems.CopyTo(selected, 0);

    // Clear listview.
    _listSelected.Items.Clear();

    // Add systems.
    ResourcePolicySystemsLVI item;
    foreach(ManagedDevice md in PolicySystemsList)
    {
       item = new ResourcePolicySystemsLVI(md);
       item.Update();
       foreach(object obj in selected)
       {
          item.Selected = (((ResourcePolicySystemsLVI)obj).Data == item.Data);
          break;
       }
       _listSelected.Items.Add(item);
    }

    // Sort list.
    _listSelected.Sort();

    // Ensure selected items are visible.
    if(_listSelected.SelectedItems.Count > 0)
    {
       _listSelected.SelectedItems[_listSelected.SelectedItems.Count 
           - 1].EnsureVisible();
       _listSelected.SelectedItems[0].EnsureVisible();
    }

    #endregion

    #region populate the available list

    // Collect selected items.
    selected = new object[_listAvailable.SelectedItems.Count];
    _listAvailable.SelectedItems.CopyTo(selected, 0);

    // Clear listview.
    _listAvailable.Items.Clear();

    // Add systems.
    ResourcePolicyAvailSystemsLVI item2;
    foreach(ManagedDevice md in AvailableSystemsList)
    {
       item2 = new ResourcePolicyAvailSystemsLVI(md);
       item2.Update();
       foreach(object obj in selected)
       {
           item2.Selected = (((ResourcePolicyAvailSystemsLVI)obj).Data 
               == item2.Data);
           break;
       }
       _listAvailable.Items.Add(item2);
    }

    // Sort list.
    _listAvailable.Sort();

    // Ensure selected items are visible.
    if(_listAvailable.SelectedItems.Count > 0)
    {
       _listAvailable.SelectedItems[_listAvailable.SelectedItems.Count 
           - 1].EnsureVisible();
       _listAvailable.SelectedItems[0].EnsureVisible();
    }

    #endregion

    _listSelected.EndUpdate();
    _listAvailable.EndUpdate();

1 个答案:

答案 0 :(得分:0)

要在第一个列之后的列中显示文本,请使用ListViewItem.SubItems集合。例如,您可以按如下方式修改主循环:

item2 = new ResourcePolicyAvailSystemsLVI(md);
// skipped some of your code
item2.SubItems.Add("My second piece of info");  // this is what puts data into the second column

我不确定您是否已将必需列添加到ListView:如果没有,则必须使用ListView.Columns.Add来显示子项。