none
how to update listview item's subitem? RRS feed

  • Question

  • Hello.

    Let me be straight out.I want to do this:

    use datareader to traverse all the listview items to see if the particular subitem of each satisfies either of my two conditions?

    If it do,I call a method to do something.In this method,for one of the two scenarios,I want to update the relative subitem of the listview.

    Here is my problem: How to update the subitem.


    I write a method to initialize th listview and give the item a name by sn there.

    and then later when needed,listview.items.find(sn,false); listviewitem item=myListview.items.find(sn,false);item.name="balabala"; item is of type listviewitem[] which find() returns,how to use ,index [0] can't work.

    Tuesday, December 6, 2011 2:20 AM

Answers

  • Hi DavidYang861,

    Welcome to the MSDN forum.

    We can use the following code block to traverse all of the listView subitems and change them value.

     foreach (ListViewItem lv in listView2.Items)
                {
                  if(lv.SubItems[0].Text =="0" &&lv.SubItems[1].Text=="1")
                  {
                      // do something. 
                  }
                  
                 }
    //also we can use this to change them value one by one.
    
     foreach (ListViewItem lv in listView2.Items)
                {
    
                  foreach (System.Windows.Forms.ListViewItem.ListViewSubItem lv1 in lv.SubItems)
                    {
                        if (lv1.Text =="0"&&lv1.Name!=null)
                        {
                            //do something
                        }
                    }
              }
    

    Hope it helps.


    Best Regards,
    Rocky Yue[MSFT]
    MSDN Community Support | Feedback to us
    • Marked as answer by DavidYang861 Thursday, December 8, 2011 6:45 AM
    Wednesday, December 7, 2011 7:18 AM
    Moderator

All replies

  • To be more specific, subitems in sn column is unique, in the process of reversal, no list view involved. The method has two parameters: sn and let

    s say statuscode

    Tuesday, December 6, 2011 2:47 AM
  • Furthermore, I use datareader to traverse, and then the data in database is updated. so use datareader to update listview is no good.

    Any help? Thank you!

    Tuesday, December 6, 2011 3:08 AM
  • Hi DavidYang861,

    Welcome to the MSDN forum.

    We can use the following code block to traverse all of the listView subitems and change them value.

     foreach (ListViewItem lv in listView2.Items)
                {
                  if(lv.SubItems[0].Text =="0" &&lv.SubItems[1].Text=="1")
                  {
                      // do something. 
                  }
                  
                 }
    //also we can use this to change them value one by one.
    
     foreach (ListViewItem lv in listView2.Items)
                {
    
                  foreach (System.Windows.Forms.ListViewItem.ListViewSubItem lv1 in lv.SubItems)
                    {
                        if (lv1.Text =="0"&&lv1.Name!=null)
                        {
                            //do something
                        }
                    }
              }
    

    Hope it helps.


    Best Regards,
    Rocky Yue[MSFT]
    MSDN Community Support | Feedback to us
    • Marked as answer by DavidYang861 Thursday, December 8, 2011 6:45 AM
    Wednesday, December 7, 2011 7:18 AM
    Moderator
  • I have solved my problem,and I don't test your code. Frankly speaking I think your reply is very good,so I mark you as answer.Thank you all the same.
    Thursday, December 8, 2011 6:49 AM
  • I have solved my problem,and I don't test your code. Frankly speaking I think your reply is very good,so I mark you as answer.Thank you all the same.

    You are welcome. It's my pleasure to help you.
    Best Regards,
    Rocky Yue[MSFT]
    MSDN Community Support | Feedback to us

    Thursday, December 8, 2011 6:51 AM
    Moderator