Question

I have a problem with my listview item. hope you can help me with that.

my listview.items.count is not working properly. 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.

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
{                
}
Was it helpful?

Solution

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"));
        }

OTHER TIPS

Count actually gives you what it says, i.e. the count. Since listview.items collection is zero indexed, set int sira = listView1.Items.Count-1;

Solution : You can Add Items to the Listview without using any Index parameter.

You need to assign first item index to ListViewItem and then add the 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"));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top