开发者

ListView returns InvalidArgument=Value of '0' - Out of Range Exception

开发者 https://www.devze.com 2022-12-30 07:22 出处:网络
I am attempting to scroll through a database of Album Names using ListView.When I initially select an album, _SelectedIndexChanged returns the appropriate album; however in subsequent attempts to sele

I am attempting to scroll through a database of Album Names using ListView. When I initially select an album, _SelectedIndexChanged returns the appropriate album; however in subsequent attempts to select an album, ListView returns a System.ArgumentOutOfRangeException - InvalidArgument=Value of '0' is not valid for 'index'. Could someone please steer me in the right direction as to what can be done to avoid this error?

private void ScrollThroughAlbums()
    {
      string selectStatement = "SELECT * FROM Albums ORDER BY Artist";
      OleDbCommand selectCommand = new OleDbCommand(selectStatement, oleDatabaseConnectionString);
      OleDbDataReader myReader = selectCommand.ExecuteReader();
      ColumnHeader columnHeader1 = new ColumnHeader();
      ColumnHeader columnHeader2 = new ColumnHeader();
      ColumnHeader columnHeader3 = new ColumnHeader();

      columnHeader1.Text = "Album";
      columnHeader2.Text = "Artist";
      columnHeader3.Text = "Tracks";

      listView1.Columns.Add(columnHeader1);
      listView1.Columns.Add(columnHeader2);
      listView1.Columns.Add(columnHeader3);
      listView1.Columns[0].Width=130;
      listView1.Columns[1].Width=130;

      listView1.View = View.Details;
      listView1.AllowColumnReorder=true;
      listView1.FullRowSelect=true;
      listView1.GridLines=tr开发者_运维技巧ue;
      listView1.MultiSelect = false;
      listView1.Sorting= SortOrder.Ascending;

      while (myReader.Read())
      {
         string frontCoverXML = myReader.GetString(3).ToString();
         string Artist = myReader.GetString(1).ToString();
         string Album = myReader.GetString(2).ToString();
         string TracksXML = myReader.GetString(4).ToString();
         ListViewItem item = new ListViewItem(new []{Album,Artist}); 
         listView1.Items.Add(item); 

        } myReader.NextResult();
        myReader.Close();
    }

   private void listView1_SelectedIndexChanged(object sender,EventArgs e)
   {
      ListView.SelectedListViewItemCollection album = this.listView1.SelectedItems;
      MessageBox.Show(album[0].ToString());
   }


   private void listView1_SelectedIndexChanged(object sender,EventArgs e)
   {
      ListView.SelectedListViewItemCollection album = this.listView1.SelectedItems;
      if(album.Count>0)
         MessageBox.Show(album[0].ToString());
   }


The error indicates the event handler fired whilst there were no items selected so your call to album[0] failed as there were no items. check to see if the count property of albumis greater than zero before popping up your message box.


It's because, behind the scenes, the selected index change event is actually called three times in the switch from one item to another. First item A is selected, then no item, then item B. A count of the indexed items referenced is maintained at each stage. In the interim step the index count is 0 and so there is nothing to reference in the index check. Trying to refer to it returns the error. Checking the count skips that middle step.

0

精彩评论

暂无评论...
验证码 换一张
取 消