Führende Nullen Datagridview

  • VB.NET

Es gibt 4 Antworten in diesem Thema. Der letzte Beitrag () ist von Zim.

    Führende Nullen Datagridview

    Hey ho
    ich versuche hier gerade dem Datagridview bei zu bringen das er führende nullen ergänzt damit er dichtig sortiert.
    Gleichzeitig soll er Zwei nullen hinten anhängen wenn nicht vorhanden.

    Aber iwie erzeugt das bei mir eine Endlosschleife.

    Hier mein Code:

    VB.NET-Quellcode

    1. Private Sub DGV_Kali_CellContentChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV_Kali.CellValueChanged
    2. If ChangeVal = False Then
    3. ChangeVal = True
    4. If DGV_Kali.Rows.Count() - 2 > 0 Then
    5. OldVal = NumericUpDown8.Value
    6. NumericUpDown8.Maximum = DGV_Kali.Rows.Count() - 2
    7. NumericUpDown7.Maximum = DGV_Kali.Rows.Count() - 2
    8. NumericUpDown8.Value = OldVal
    9. NumericUpDown7.Value = OldVal
    10. FührendeNullY = 0
    11. FührendeNullX = 0
    12. For i = 0 To DGV_Kali.Rows.Count() - 2
    13. DGV_Kali.Rows(i).Cells(0).Value = Convert.ToDouble(VB.Replace(DGV_Kali.Rows(i).Cells(0).Value.ToString, ".", ",")).ToString
    14. If DGV_Kali.Rows(i).Cells(0).Value.ToString.IndexOf(",") = -1 Then
    15. DGV_Kali.Rows(i).Cells(0).Value = DGV_Kali.Rows(i).Cells(0).Value.ToString & ",00"
    16. End If
    17. If FührendeNullX < DGV_Kali.Rows(i).Cells(0).Value.ToString.IndexOf(",") And DGV_Kali.Rows(i).Cells(0).Value.ToString.IndexOf(",") > -1 Then
    18. FührendeNullX = DGV_Kali.Rows(i).Cells(0).Value.ToString.IndexOf(",")
    19. End If
    20. If (DGV_Kali.Rows(i).Cells(1).Value Is Nothing) = False Then
    21. DGV_Kali.Rows(i).Cells(1).Value = Convert.ToDouble(VB.Replace(DGV_Kali.Rows(i).Cells(1).Value.ToString, ".", ",")).ToString
    22. If DGV_Kali.Rows(i).Cells(1).Value.ToString.IndexOf(",") = -1 Then
    23. DGV_Kali.Rows(i).Cells(1).Value = DGV_Kali.Rows(i).Cells(1).Value.ToString & ",00"
    24. End If
    25. If FührendeNullY < DGV_Kali.Rows(i).Cells(1).Value.ToString.IndexOf(",") And DGV_Kali.Rows(i).Cells(1).Value.ToString.IndexOf(",") > -1 Then
    26. FührendeNullY = DGV_Kali.Rows(i).Cells(1).Value.ToString.IndexOf(",")
    27. End If
    28. End If
    29. Next
    30. For j = 0 To DGV_Kali.Rows.Count() - 2
    31. While DGV_Kali.Rows(j).Cells(0).Value.ToString.IndexOf(",") < FührendeNullX And DGV_Kali.Rows(j).Cells(0).Value.ToString.IndexOf(",") > -1
    32. DGV_Kali.Rows(j).Cells(0).Value = ("0" & DGV_Kali.Rows(j).Cells(0).Value.ToString).ToString
    33. End While
    34. If (DGV_Kali.Rows(j).Cells(1).Value Is Nothing) = False Then
    35. While DGV_Kali.Rows(j).Cells(1).Value.ToString.IndexOf(",") < FührendeNullY And DGV_Kali.Rows(j).Cells(1).Value.ToString.IndexOf(",") > -1
    36. DGV_Kali.Rows(j).Cells(1).Value = ("0" & DGV_Kali.Rows(j).Cells(1).Value.ToString).ToString
    37. End While
    38. End If
    39. Next
    40. End If
    41. End If
    42. ChangeVal = False
    43. End Sub


    Jemand eine Idee wie man es besser machen kann ?
    Das kannst Du alles in ToString erledigen:

    VB.NET-Quellcode

    1. Dim d As Double = 11.220000000000001
    2. Dim txt As String = d.ToString("0000.0000")
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    :)

    VB.NET-Quellcode

    1. Private Sub DGV_Kali_CellContentChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV_Kali.CellValueChanged
    2. If ChangeVal = False Then
    3. ChangeVal = True
    4. If DGV_Kali.Rows.Count() - 2 > 0 Then
    5. OldVal = NumericUpDown8.Value
    6. NumericUpDown8.Maximum = DGV_Kali.Rows.Count() - 2
    7. NumericUpDown7.Maximum = DGV_Kali.Rows.Count() - 2
    8. NumericUpDown8.Value = OldVal
    9. NumericUpDown7.Value = OldVal
    10. FührendeNullY = 0
    11. FührendeNullX = 0
    12. For i = 0 To DGV_Kali.Rows.Count() - 2
    13. DGV_Kali.Rows(i).Cells(0).Value = Convert.ToDouble(VB.Replace(DGV_Kali.Rows(i).Cells(0).Value.ToString, ".", ",")).ToString
    14. DGV_Kali.Rows(i).Cells(0).Value = Convert.ToDouble(DGV_Kali.Rows(i).Cells(0).Value).ToString("###0.00###")
    15. If FührendeNullX < DGV_Kali.Rows(i).Cells(0).Value.ToString.IndexOf(",") And DGV_Kali.Rows(i).Cells(0).Value.ToString.IndexOf(",") > -1 Then
    16. FührendeNullX = DGV_Kali.Rows(i).Cells(0).Value.ToString.IndexOf(",")
    17. End If
    18. If (DGV_Kali.Rows(i).Cells(1).Value Is Nothing) = False Then
    19. DGV_Kali.Rows(i).Cells(1).Value = Convert.ToDouble(VB.Replace(DGV_Kali.Rows(i).Cells(1).Value.ToString, ".", ",")).ToString
    20. DGV_Kali.Rows(i).Cells(1).Value = Convert.ToDouble(DGV_Kali.Rows(i).Cells(1).Value).ToString("###0.00###")
    21. If FührendeNullY < DGV_Kali.Rows(i).Cells(1).Value.ToString.IndexOf(",") And DGV_Kali.Rows(i).Cells(1).Value.ToString.IndexOf(",") > -1 Then
    22. FührendeNullY = DGV_Kali.Rows(i).Cells(1).Value.ToString.IndexOf(",")
    23. End If
    24. End If
    25. Next
    26. NullY = ""
    27. For i = 1 To FührendeNullY
    28. NullY = "0" & NullY
    29. Next
    30. NullX = ""
    31. For i = 1 To FührendeNullX
    32. NullX = "0" & NullX
    33. Next
    34. For j = 0 To DGV_Kali.Rows.Count() - 2
    35. DGV_Kali.Rows(j).Cells(0).Value = Convert.ToDouble(DGV_Kali.Rows(j).Cells(0).Value).ToString(NullX & ".00###")
    36. If (DGV_Kali.Rows(j).Cells(1).Value Is Nothing) = False Then
    37. DGV_Kali.Rows(j).Cells(1).Value = Convert.ToDouble(DGV_Kali.Rows(j).Cells(1).Value).ToString(NullY & ".00###")
    38. End If
    39. Next
    40. End If
    41. ChangeVal = False
    42. End If
    43. End Sub