简体   繁体   English

Listview.count-InvalidArgument =值“ 0”对“索引”无效

[英]Listview.count - InvalidArgument=Value of '0' is not valid for 'index

I have a problem with my listview item. 我的listview项目有问题。 hope you can help me with that. 希望你能帮助我。

my listview.items.count is not working properly. 我的listview.items.count工作不正常。 EventhoughI have 1 data, that suits my sql-string, (I checked it from db) count comes zero and i'm getting "InvalidArgument=Value of '0' is not valid for 'index" error. 即使我有1个数据,也适合我的sql字符串,(我从db检查了它)计数为零,并且我收到"InvalidArgument=Value of '0' is not valid for 'index" error.

I dont know what i'm doing wrong? 我不知道我在做什么错? here's my code; 这是我的代码;

try
{
     mcon.Open();
     reader = comma.ExecuteReader();
     while (reader.Read())
     {
          int sira = listView1.Items.Count;
          listView1.Items[sira].SubItems.Add(reader.GetString("id"));
          listView1.Items[sira].SubItems.Add(reader.GetString("ad"));
          listView1.Items[sira].SubItems.Add(reader.GetString("soyad"));
          listView1.Items[sira].SubItems.Add(reader.GetString("evrakulastimi"));
          listView1.Items[sira].SubItems.Add(reader.GetString("basvurusonuclandimi"));
     }
}
catch
{                
}

Count actually gives you what it says, ie the count. 伯爵实际上给你它说的话,即伯爵。 Since listview.items collection is zero indexed, set int sira = listView1.Items.Count-1; 由于listview.items集合的索引为零,所以设置int sira = listView1.Items.Count-1;

Probably, you've forgot to add a new item : 可能是您忘记添加新项目

      while (reader.Read())
        {
            int sira = listView1.Items.Count;

            listView1.Items.Add("Put some text here"); // <- Add a new item

            listView1.Items[sira].SubItems.Add(reader.GetString("id"));
            listView1.Items[sira].SubItems.Add(reader.GetString("ad"));
            listView1.Items[sira].SubItems.Add(reader.GetString("soyad"));
            listView1.Items[sira].SubItems.Add(reader.GetString("evrakulastimi"));
            listView1.Items[sira].SubItems.Add(reader.GetString("basvurusonuclandimi"));
        }

Solution : You can Add Items to the Listview without using any Index parameter. 解决方案:您可以在不使用任何Index参数的情况下将项目添加到Listview

You need to assign first item index to ListViewItem and then add the SubItems . 您需要将第一个项目index分配给ListViewItem ,然后添加SubItems

Try This : 尝试这个 :

    ListViewItem lvi = listView1.Items.Add(reader.GetString("id"));
    lvi.SubItems.Add(reader.GetString("ad"));
    lvi.SubItems.Add(reader.GetString("soyad"));
    lvi.SubItems.Add(reader.GetString("evrakulastimi"));
    lvi.SubItems.Add(reader.GetString("basvurusonuclandimi"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ListView中的错误:InvalidArgument =值&#39;0&#39;对&#39;index&#39;无效 - Error in ListView: InvalidArgument = Value of '0' is not valid for 'index' invalidargument =值&#39;8&#39;对于&#39;索引&#39;无效 - invalidargument=value of '8' is not valid for 'index' InvalidArgument =值&#39;4&#39;对于&#39;索引&#39;无效 - InvalidArgument=Value of '4' is not valid for 'index' InvalidArgument =值&#39;1&#39;对&#39;索引&#39;无效 - InvalidArgument=Value of '1' is not valid for 'index' InvalidArgument =值&#39;3&#39;对于&#39;索引&#39;无效。 参数名称:索引 - InvalidArgument=Value of'3' is not valid for 'index'. parameter name: index InvalidArgument=&#39;-1&#39; 的值对 &#39;index&#39; 无效。 参数名称:索引 - InvalidArgument=Value of '-1' is not valid for 'index'. Parameter name: index 附加信息:InvalidArgument =值“ 0”对“索引”无效 - Additional information: InvalidArgument=Value of '0' is not valid for 'index' c#InvalidArgument =值&#39;-1&#39;对&#39;索引&#39;无效 - c# InvalidArgument=Value of '-1' is not valid for 'index' InvalidArgument = '0' 的值对'index' 无效,当尝试仅获取 ListView 中的选定项目时 - InvalidArgument=Value of '0' is not valid for 'index' when trying to get only selected item in a ListView InvalidArgument =值&#39;6&#39;对于索引无效。 参数名称:索引 - InvalidArgument=Value of '6' is not valid for index. Parameter name: index
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM