Autor Beitrag
dragi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 88



BeitragVerfasst: Fr 27.01.06 11:32 
Hallo,

ich habe folgendes Problem: Ich lese aus einer Datenbank Datensätze die ich in einer Listview als Tabelle auslese. Ich habe 3 Spalten in meiner Listview wobei die erste aber nciht sichtbar ist. FullRowSelect ist aktiviert und wenn der User einen Datensatz anklickt möchte ich en Wert aus der 1. Spalte in eine Variable lesen. Beim ersten anklicken klappt das aber sobald ich wieder einen Datensatz anklicke kommt eine Exception die da lautet:

ArgumentOutofRangeException was unhandeld!
InvalidArgument=Value of '0' is not valid for 'index'. Parameter name: index

Hier bestücke ich die listView:

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
public Boolean refreshListView()
        {
            listView1.Items.Clear();
            cmd = new SqlCommand();
            cmd.Connection = MainForm.con;
            cmd.CommandText = "SELECT * FROM employee ORDER BY name, forename";
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                try
                {
                    ListViewItem MyItem = new ListViewItem(dr["ID"].ToString());
                    MyItem.SubItems.Add(dr["name"].ToString());
                    MyItem.SubItems.Add(dr["forename"].ToString());
                    listView1.Items.Add(MyItem);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Fehler 2: " + ex);
                    return false;
                }
            }
            dr.Close();
            cmd = null;
            return true;
        }


Hier bestücke ich die Variable und wenn das programm das zweite mal rein läuft kommt die Exception:

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            myID = listView1.SelectedItems[0].Text;
            MessageBox.Show(myID);
        }


Was mache ich falsch? Kann mir jemand helfen?

Gruß

dragi

Moderiert von user profile iconChristian S.: Code- durch C#-Tags ersetzt
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Fr 27.01.06 11:41 
Mal geschaut, ob SelectedItems überhaupt ein Element enthält? Das sollte man vorher immer abfangen!

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Xqgene
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 20



BeitragVerfasst: Fr 27.01.06 11:41 
hier ein kleiner Hinweiß:

ItemSelectionChanged Event
Zitat:
The ItemSelectionChanged event occurs whether the item state changes from selected to deselected or deselected to selected.

_________________
There are only 10 types of people in the world:
Those who understand Binary and those who don`t.