1

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
{                
}
1
  • item with index "listView1.Items.Count" never exist, you should either address to "listView1.Items.Count - 1" item or add an item after int sira = listView1.Items.Count; Commented Dec 18, 2013 at 12:20

3 Answers 3

3

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"));
        }
Sign up to request clarification or add additional context in comments.

Comments

3

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;

1 Comment

thank you for your time, but the "add" property solved my problem
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.