[英]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.