ListView 返回 InvalidArgument=“0”的值; - 超出范围异常

发布于 2024-09-01 00:07:42 字数 1885 浏览 6 评论 0原文

我正在尝试使用 ListView 滚动浏览专辑名称数据库。当我最初选择一个专辑时,_SelectedIndexChanged 返回相应的专辑;但是,在随后尝试选择专辑时,ListView 返回 System.ArgumentOutOfRangeException - InvalidArgument=“0”值对于“index”无效。有人可以引导我走向正确的方向,了解如何避免此错误吗?

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=true;
      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());
   }

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=true;
      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());
   }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

变身佩奇 2024-09-08 00:07:42
   private void listView1_SelectedIndexChanged(object sender,EventArgs e)
   {
      ListView.SelectedListViewItemCollection album = this.listView1.SelectedItems;
      if(album.Count>0)
         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());
   }
迟到的我 2024-09-08 00:07:42

该错误表示在没有选择任何项目时触发了事件处理程序,因此您对 album[0] 的调用失败,因为没有项目。在弹出消息框之前检查 album 的 count 属性是否大于零。

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.

尽揽少女心 2024-09-08 00:07:42

这是因为,在幕后,选择的索引更改事件实际上在从一项切换到另一项时被调用了三次。首先选择项目 A,然后不选择项目,然后选择项目 B。在每个阶段都会维护引用的索引项目的计数。在中间步骤中,索引计数为 0,因此索引检查中没有任何内容可供参考。尝试引用它会返回错误。检查计数会跳过中间步骤。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文