CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2010
    Posts
    4

    Talking ADO.net ListView in C#

    Hi Guys... need some help.. I have this code..

    private void listView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    MessageBox.Show(listView1.SelectedItems[0].SubItems[0].Text);

    }

    and I always get this kind of error message evry time I change the selected Item im my listbox...

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

    i need a way to get each value for the selected item in my listbox so that I can use it as a parameter for my sqlcommand.

    need help.. =)

  2. #2
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: ADO.net ListView in C#

    Check SelectedItems and SubItems if they contain something, because [0] means first element, which may not be presented (e.g. if the list is empty).
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  3. #3
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: ADO.net ListView in C#

    Just to add to what boudino has said...you should run it through the debugger and inspect the contents of 'SelectedItems' and 'SubItems' at runtime. They may not be what you expect. If there is any chance those collections could be empty you should put a test around access to the values (e.g. using an 'if' statement...)

  4. #4
    Join Date
    Mar 2010
    Posts
    4

    Re: ADO.net ListView in C#

    Ok I'l go and check it out...

  5. #5
    Join Date
    Mar 2010
    Posts
    4

    Re: ADO.net ListView in C#

    why is it that every time I change the selected item in my listview using the SelectedIndexChanged event, it resets its value to nothing?

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
  •  





Click Here to Expand Forum to Full Width

Featured