Autor Beitrag
tomycat
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 265
Erhaltene Danke: 1



BeitragVerfasst: Fr 01.04.16 20:52 
hallo,
per Button wird folgendes in die erste Zeile geschrieben:
Spalte 0 -> 1
Spater 1 -> "blalbl"
Spalte 2 -> 100
Spalte 3 -> 19
Spalte 4 und 5 wird nicht angefasst.

Dann drücke ich folgenden Button:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
        private void button41_Click(object sender, EventArgs e)
        {
       

            for (int counter = 0; counter < (dataGridView1.Rows.Count); counter++)
            {

                ///////////////////////////////////////
                // 
                if (dataGridView1.Rows[counter].Cells[3].Value != null && dataGridView1.Rows[counter].Cells[5].Value != null)
                {

                    MessageBox.Show(dataGridView1.Rows[counter].Cells[5].Value.ToString());
                    // Cell 5 wird weiterverarbeitet und dann kommt es zum crash.

                }

            }
        }

Wenn Cell 3 und 5 NICHT leer ist, DAAAAAAANNNNNNNNNN soll die Messagesbox angehen !!!!
Es kommt aber trotzdem die Messagesbox und weisem Inhalt.
Dann kommt der Crash.
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 01.04.16 21:19 
Was sagt der Debugger denn, was in dataGridView1.Rows[counter].Cells[5].Value drin steht?

Und an welcher Stelle crasht es dann?

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".

Für diesen Beitrag haben gedankt: tomycat
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4700
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Fr 01.04.16 21:31 
Bedenke null und z.B. eine leerer string sind 2 verschiedene Sachen.

Für diesen Beitrag haben gedankt: tomycat
tomycat Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 265
Erhaltene Danke: 1



BeitragVerfasst: Fr 01.04.16 22:11 
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
  for (int counter = 0; counter < (dataGridView1.Rows.Count); counter++)
            {

                ///////////////////////////////////////
                // 
                if (!(String.IsNullOrEmpty(dataGridView1.Rows[counter].Cells[5].Value.ToString())))
                {

                    MessageBox.Show(dataGridView1.Rows[counter].Cells[5].Value.ToString());
                    // Cell 5 wird weiterverarbeitet und dann kommt es zum crash.

                }

            }

NullReferenceException wurde nicht behandelt.

Warum geht das nicht? Gleiches Spiel nochmal, warum kommt es jetzt nicht zur Ausgabe der MessagesBox? Der Crash kommt jetzt vorher.

PS Aus Cell5 wird eine Float gemacht, das kann nicht mit leerem Inhalt funktionieren.
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4700
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Fr 01.04.16 22:28 
In dem Fall wo value doch null ist kannst du nicht ToString() aufrufen das würde eine NullReferenceException auslösen.

Wenn du von Spalte 5 den Datentyp weißt dann caste auf diesen Typ (mit dem as-Operator) und prüfen dann ohne ToString oder sonstwas.

Für diesen Beitrag haben gedankt: tomycat