none
Set the properties of a ListViewItem

    Question

  • Hi,

    Can anyone please tell me how to change the properties/attibutes of a line in a ListView at runtime?
    Basically my form displays minutes worked for a day in a list view and if the hours are less than
    7.5 hrs I want to display in bold and/or a strong colour. 

    I'm assuming that I need to set the property against the Listview1 not the listviewitems as the latter are basically the cells?

    I also thought that I would need to keep track of the index (idx) in a seperate int variable so that I can
    reference the listview line explicitly, problem is I'm getting this message...

    InvalidArgument=Value of '1' is not valid for 'index'.
    Parameter name: index


    Here is the code.  I've tried setting idx=0 and idx=1 at the start but the error is the same.

    int idx = 1; 
    foreach (RegisterList rl in registerData)
    {
    ListViewItem listviewitem = new ListViewItem(rl.inDate.ToString());
    listviewitem.SubItems.Add(rl.registerTimeIn.ToString());
    listviewitem.SubItems.Add(rl.registerTimeOut.ToString());
    listviewitem.SubItems.Add(rl.minutesWorked.ToString());
    listviewitem.SubItems.Add(rl.difference.ToString());
    listviewitem.SubItems.Add(rl.flag.ToString());

    if (rl.over) //(!rl.over)
    {


    listView1.Items[idx].Font =
    new Font(listView1.Items[idx].Font, System.Drawing.FontStyle.Bold);

    }

    this.listView1.Items.Add(listviewitem);

    idx++;
    }

    Any examples of how to do this on a line by line basis would be great.

    Regards
    Dave

    Monday, August 31, 2009 12:01 PM

Answers

  • You are trying to access a list item from the ListView that does not exist. You cannot access it from the listView1.Items collection before adding it using Add().
    Setting the listviewitem.Font or going through the ListView makes no difference, the result is the same.

    			ListViewItem item = new ListViewItem("Some item");
    			item.Font = new Font(item.Font, FontStyle.Bold);
    
    			this.listView1.Items.Add(item);
    


    /Calle
    - Still confused, but on a higher level -
    • Marked as answer by york0001 Monday, August 31, 2009 12:48 PM
    Monday, August 31, 2009 12:40 PM

All replies

  • You should be able to set the property directly on listviewitem. No need to go via listView1.Items[idx].
    Monday, August 31, 2009 12:18 PM
  • But if you do want to go via listView1.Items[idx], you need to add the item before trying to access it (and start the index on 0).
    Monday, August 31, 2009 12:33 PM
  • You are trying to access a list item from the ListView that does not exist. You cannot access it from the listView1.Items collection before adding it using Add().
    Setting the listviewitem.Font or going through the ListView makes no difference, the result is the same.

    			ListViewItem item = new ListViewItem("Some item");
    			item.Font = new Font(item.Font, FontStyle.Bold);
    
    			this.listView1.Items.Add(item);
    


    /Calle
    - Still confused, but on a higher level -
    • Marked as answer by york0001 Monday, August 31, 2009 12:48 PM
    Monday, August 31, 2009 12:40 PM
  • You are trying to access a list item from the ListView that does not exist. You cannot access it from the listView1.Items collection before adding it using Add().
    Setting the listviewitem.Font or going through the ListView makes no difference, the result is the same.

    			ListViewItem item = new
     ListViewItem("Some item"
    );
    			item.Font = new
     Font(item.Font, FontStyle.Bold);
    
    			this
    .listView1.Items.Add(item);
    
    


    /Calle
    - Still confused, but on a higher level -

    Thanks, that works fine.  Setting the colour to yellow was pretty easy as well. 

    listviewitem.BackColor = Color.Yellow;

    Thanks to all who repsonded.

    Dave
     



    Monday, August 31, 2009 12:48 PM