[RESOLVED] Add an Item to a ListView in VitualMode ?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: [RESOLVED] Add an Item to a ListView in VitualMode ?

  1. #1
    Join Date
    Jun 2008
    Posts
    57

    [RESOLVED] Add an Item to a ListView in VitualMode ?

    Hi,
    I'm gonna add an item to a ListView in VirtualMode.
    Below is my C# code to do that :

    Code:
    listView1.VirtualMode = true;
    listView1.VirtualListSize = query.Count();
    foreach (var item in query)
    {
        i++;
        string tableNumber = item.TableNumber.HasValue ? item.TableNumber.Value.ToString() : "";
        ListViewItem newItem = new ListViewItem(new string[] { i.ToString(), 
                               item.FactorType.ToString(), 
                               item.FactorNumber.ToString(),
                               tableNumber.ToString(),
                               item.Price.ToString(), 
                               DateTime.Now.ToString() });
        newItem.Font = new Font("Tahoma", 12);
        newItem.Name = "Item" + i.ToString();
    
        listView1.Items.Add(newItem);//Exception
    }
    
    void listView1_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
    {
        e.Item = new ListViewItem(e.ItemIndex.ToString());
    }
    The Exception :
    Code:
    When the ListView is in virtual mode, you cannot add items to the ListView items collection. Use  the VirtualListSize property instead to change the size of the ListView items collection.
    Would you please guide me ?
    Thanks.
    Last edited by M-Dayyan; January 16th, 2010 at 06:30 AM.

  2. #2
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Add an Item to a ListView in VitualMode ?

    the ListView.VirtualMode documentation clearly states that
    Quote Originally Posted by msdn
    In order to use virtual mode, you must handle the RetrieveVirtualItem event, which is raised every time the ListView requires an item. This event handler should create the ListViewItem object that belongs at the specified index.
    ListView.RetrieveVirtualItem

    you don't follow the examples
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  3. #3
    Join Date
    Jun 2008
    Posts
    57

    Re: Add an Item to a ListView in VitualMode ?

    I've done it, I forgot to post :

    Code:
    void listView1_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
    {
        e.Item = new ListViewItem(e.ItemIndex.ToString());
    }
    Last edited by M-Dayyan; January 16th, 2010 at 06:34 AM.

  4. #4
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Add an Item to a ListView in VitualMode ?

    so why are trying to add new items like that
    Code:
    listView1.Items.Add(newItem);//Exception
    if this is not allowed in virtual mode?
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  5. #5
    Join Date
    Jun 2008
    Posts
    57

    Re: Add an Item to a ListView in VitualMode ?

    My question is exactly it,
    How can I add a new item to a listview in VirtualMode ?

  6. #6
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Add an Item to a ListView in VitualMode ?

    read my first response here, this is the second post (#2) you're even doing the same. I don't understand where is the problem? have you not subscribed to the event?
    Code:
    listView1.RetrieveVirtualItem += new RetrieveVirtualItemEventHandler(this.listView1_RetrieveVirtualItem)
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  7. #7
    Join Date
    Jun 2008
    Posts
    57

    Re: Add an Item to a ListView in VitualMode ?

    Thanks memeloo.
    I have done it too !!!
    I think you couldn't understand me well ! let's explain more :

    I'm working with SQL to LINQ,
    before I use ListView in VirtulMode, whenever I was getting a query with LINQ , I added them to the ListView with a loop :
    PHP Code:
    foreach (var item in query)
    {
        
    i++;
        
    string tableNumber item.TableNumber.HasValue item.TableNumber.Value.ToString() : "";
        
    ListViewItem newItem = new ListViewItem(new string[] { i.ToString(), 
                               
    item.FactorType.ToString(), 
                               
    item.FactorNumber.ToString(),
                               
    tableNumber.ToString(),
                               
    item.Price.ToString(), 
                               
    DateTime.Now.ToString() });
        
    newItem.Font = new Font("Tahoma"12);
        
    newItem.Name "Item" i.ToString();

        
    listView1.Items.Add(newItem);//Exception

    The mount of data was too large, so I decided to use VirtualMode.
    Next, I implemented listView1_RetrieveVirtualItem as I read it on MSDN.

    PHP Code:
    listView1.VirtualMode true;
    listView1.VirtualListSize query.Count();

    listView1.RetrieveVirtualItem += new RetrieveVirtualItemEventHandler(this.listView1_RetrieveVirtualItem)
    void listView1_RetrieveVirtualItem(object senderRetrieveVirtualItemEventArgs e)
    {
        
    e.Item = new ListViewItem(e.ItemIndex.ToString());

    But now I receive an exception on the line that I marked it in above foreach loop.

    My Problem :
    I don't now, how I can add new item in VitualMode ?

    Do you understand what I mean ?

  8. #8
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Add an Item to a ListView in VitualMode ?

    I do understand. you do not populate the entire list in a loop like before. it is automatically populated as needed in the listView1_RetrieveVirtualItem event handler. there you must put the code that retrieves the right items.

    you can cache the result from the query so that you don't have to run the query every time.
    Last edited by memeloo; January 16th, 2010 at 07:08 AM.
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  9. #9
    Join Date
    Jun 2008
    Posts
    57

    Re: Add an Item to a ListView in VitualMode ?

    You mean I have to put foreach contents in listView1_RetrieveVirtualItem method ?

  10. #10
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Add an Item to a ListView in VitualMode ?

    no. you get the e.ItemIndex which corresponds to the listView's item that will be displayed next.

    I don't know what your query result looks like, but you have to do something like this:
    Code:
    void listView1_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e) 
    { 
        // var query = from ... // run the query here or cache it somewhere else and just use the cache    
        e.Item = new ListViewItem(query[e.ItemIndex]);
    }
    but probably this won't work. your query result has to be some array or a list and you must get the item at the e.ItemIndex position.
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  11. #11
    Join Date
    Jun 2008
    Posts
    57

    Re: Add an Item to a ListView in VitualMode ?

    Excellent ,
    I got it my friend.
    Thanks a lot

Tags for this Thread

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center