CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2002
    Posts
    5,757

    ListView Fundamentals :: C#

    Hi.

    I have some fundamental questions on the listview.

    1) I added a listview with some columns, however, the column titles do not appear. The columns are there, but they do not appear. Is there a setting in winform that can hide it?

    2) How do you search for an item in the listview?

    3) How do you set the text of a specific item in a listview? For example, I want to set the text of item 2 column 1.

    Thanks,
    Kuphryn

  2. #2
    Join Date
    Sep 1999
    Posts
    67
    I might have the answer for your first question,

    ColumnHeader header1 = this.listviewInstances.Columns.Add("C1", 10, HorizontalAlignment.Center);
    ColumnHeader header2 = this.listviewInstances.Columns.Add("C2", 10, HorizontalAlignment.Center);
    ColumnHeader header3 = this.listviewInstances.Columns.Add("C3", 10, HorizontalAlignment.Center );
    ColumnHeader header4 = this.listviewInstances.Columns.Add("C4", 10, HorizontalAlignment.Center );

    Hope it helps

    /Anders

  3. #3
    Join Date
    Dec 2000
    Location
    Slovakia
    Posts
    1,043
    2.) If you need to search for item based on its name, you have to iterate through list view item collection to find some item.

    Code:
    ListViewItem lvi = null;
    string strYourText = "some text";
    
    foreach (ListViewItem _lvi in this.listYourList.Items)
    {
        if (_lvi.Text == strYourText)
        {
            lvi = _lvi;
            break;
        }
    }
    
    if (lvi != null)
    {
        // We found it... lvi is the proper ListViewItem 
    }
    If you need to search alse the subitems, you need to make this searching also on the _lvi.SubItems collection...

    3.)
    Code:
    public void SetListItemText(ListView list, int iItemIndex, int iSubItemIndex, string strNewText)
    {
        if (list == null)
            return;
    
        if (iItemIndex < 0 || iItemIndex >= list.Items.Count)
            throw new ArgumentOutOfRangeException("iItemIndex", "Parameter is out of range");
    
        ListViewItem lvi = list.Items[iItemIndex];
        if (lvi == null)
           return;
    
        if (iSubItemIndex < 0 || iSubItemIndex >= lvi.SubItems.Count)
            throw new ArgumentOutOfRangeException("iSubItemIndex", "Parameter is out of range");
    
        ListViewItem lviSubItem = lvi.SubItems[iSubItemIndex];
        if (lviSubItem != null)
            lviSubItem.Text = strNewText;
    }
    Martin

  4. #4
    Join Date
    Dec 2000
    Location
    Slovakia
    Posts
    1,043
    Additionally, call
    Code:
    this.listviewInstances.Columns.Clear();
    before you write the code posted by Anders Jansson...

    Martin

  5. #5
    Join Date
    Feb 2002
    Posts
    5,757
    Okay. Thanks.

    Kuphryn

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured