views:

32

answers:

1

Adding contents to listview is a simple proceess like

   ListViewItem item = new ListViewItem();
            listView1.Items.Add(item);
            item.Text = "fdfdfd";
            item.SubItems.Add("melp");
            item.SubItems.Add("asfd");

Can any one tell me what exactly going on here though here also contents are added and displayed,,this bit of code i am taking from my project

protected override void OnUpdate()
     {
        string func = "ResourcePolicySystemsLVI.OnUpdate";
        try
        {
           if(Data != null)
           {
              Text = base.Data.Name;
              if(SubItems.Count == 1)
              {
                 SubItems.Add(((IResourcePolicy)Data).ResourcePolicyEnabled.ToString());  // ResourcePolicyEnabled and ResourcePolicyCurrent are attributes
                 SubItems.Add(((IResourcePolicy)Data).ResourcePolicyCurrent.ToString());
              }
              else
              {
                 SubItems[1].Text = ((IResourcePolicy)Data).ResourcePolicyEnabled.ToString();
                 SubItems[2].Text = ((IResourcePolicy)Data).ResourcePolicyCurrent.ToString();
              }
           }
           base.OnUpdate();
        }


     /// <summary>
     /// The IResourcePolicy interface of the ManagedDevice associated with this ListViewItem.
     /// </summary>
     public new IResourcePolicy Data
     {
        get
        {
           return (IResourcePolicy)base.Data;
        }
     }
A: 

Well, it is checking the SubItems count. If there is only 1 sub item, it will add 2 new sub items with the PolicyEnabled/PolicyCurrent strings. Otherwise, it's assuming that there is greater than 1 SubItem and just setting the Text (value) of the SubItems to the new values. I think really it should be if(SubItems.Count > 1). So I think basically on first run through it will find no sub items, create 2 new sub items. Then on every subsequent call it will simply update the existing sub items rather than adding more

Actually, I think the SubItem.Count check should be > 0, as initially there will be no SubItems. If that code is actually working, I'm assuming the SubItems are being created before that function is ever called and hence why it never breaks. Overall it looks like it will break pretty easy.

mrnye
thanks for you valuable information which cleared my dobts
peter