Datagridview RowFilter funktioniert nicht richtig

  • VB.NET
  • .NET (FX) 4.0

Es gibt 9 Antworten in diesem Thema. Der letzte Beitrag () ist von VaporiZed.

    Datagridview RowFilter funktioniert nicht richtig

    Hallo zusammen,

    ich habe folgenden Code um ein Datagridview zu filtern.


    dv.RowFilter = "gr < 500"
    dv.Sort = "Pos ASC"
    DataGridView1.DataSource = dv

    Im Datagridview bleibt eine Zeile die im Feld "gr" = 856 hat.
    Da der Filter aber <500 ist darf diese Zeile da nicht stehen.

    Klicke ich aber auf ein leeres Feld in der nächsten Zeile verschwindet der eine Datensatz aus dem Datagridview.

    Kann mir da jemand helfen?
    Ich kann das Problem nicht nachstelle, bei mir geht es wie gewünscht. Wie wird das DataView befüllt und womit die DataTable? Welche Datentypen haben die Spalten in der DataTable?
    Dieser Beitrag wurde bereits 5 mal editiert, zuletzt von „VaporiZed“, mal wieder aus Grammatikgründen.

    Aufgrund spontaner Selbsteintrübung sind all meine Glaskugeln beim Hersteller. Lasst mich daher bitte nicht den Spekulatiusbackmodus wechseln.
    Hallo,

    ich hab mal mehr code eingefügt. Column index 30 ist genau die Spalte nach der gefiltert wird.


    VB.NET-Quellcode

    1. Using connection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Excel1 & ";Extended Properties=Excel 12.0;")
    2. connection.Open()
    3. Using dataadapter = New OleDbDataAdapter("Select posnr as Pos, Anz as Stk, KZ, BEZ as Bezeichnung, Kennzahl as F, AG, A, B, C, D, E, F as F1, L, W, R, G, H, M, N, X, Y, Ra1Vt as 'Q1/V1' , Ra2Vt as 'Q2/V2', Ra3Vt as 'Q3/V3', round(IsoOf,2) as Isolierung, IsoArt, MA as Material, MS as 'MatS mm', Bem, round([OF],2) as OF1, 0 as gr From [Tabelle1$] where kz like '%' order by posnr", connection)
    4. tbl = New DataTable("Tabelle1")
    5. dataadapter.Fill(tbl)
    6. DataGridView1.DataSource = tbl
    7. End Using
    8. End Using
    9. End If
    10. For Each row As DataGridViewRow In DataGridView1.Rows
    11. If row.Cells(2).Value = "BO" Then
    12. row.Cells(30).Value = grosste_zahl(row.Cells(6).Value, row.Cells(7).Value, 0, 0, 0) * 1
    13. row.Cells(30).Value = Int32.Parse(row.Cells(30).Value)
    14. End If
    15. Next
    16. Dim dv As DataView = DirectCast(DataGridView1.DataSource, DataTable).DefaultView
    17. Dim xls_Appl As Excel.Application ' Excel Anwendung
    18. Dim xls_Mappe As Excel.Workbook ' Excel Arbeitsmappe
    19. If CheckBox1.Checked = False Then
    20. dv.RowFilter = "gr < 500"
    21. dv.Sort = "Pos ASC"
    22. DataGridView1.DataSource = dv


    CodeTags hinzugefügt ~VaporiZed

    Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von „VaporiZed“ ()

    1.

    VaporiZed schrieb:

    Welche Datentypen haben die Spalten in der DataTable?

    2. Ich hab mal CodeTags hinzugefügt, bitte zukünftig selber CodeTags verwenden
    3. Der Code ist unvollständig, da in Zeile#26 ein If-Block beginnt, aber kein End If mehr auftaucht.
    4. Was ist in Z#15 grosste_zahl?
    5. Bevor wir jetzt hier versuchen, das Problem mit einer Excel-Tabelle nachzustellen, dampf mal bitte die Excel-Tabelle auf das Wesentliche ein, nämlich die Spalte gr (was immer auch sich dahinter verbergen mag) und lass den ganzen Code weg, der für das Problem nicht relevant ist. Hat die For-Schleife einen Einfluss auf gr?
    Dieser Beitrag wurde bereits 5 mal editiert, zuletzt von „VaporiZed“, mal wieder aus Grammatikgründen.

    Aufgrund spontaner Selbsteintrübung sind all meine Glaskugeln beim Hersteller. Lasst mich daher bitte nicht den Spekulatiusbackmodus wechseln.
    Also ich hab jetzt mal den kompletten Code au0er dem Excel Teil. Das gefilterte Datagridview wird dann nur noch nach Excel geschoben.
    Der Datentyp sollte doch Int32 sein.

    VB.NET-Quellcode

    1. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    2. startzeit = Now
    3. OpenFileDialog1.Filter = "All Files (*.*)|*.*|Excel files (*.xlsx)|*.xlsx|CSV Files (*.csv)|*.csv|XLS Files (*.xls)|*xls"
    4. Dim tbl
    5. If (OpenFileDialog1.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
    6. Dim fileinfo As New FileInfo(OpenFileDialog1.FileName)
    7. Dim FileName As String = OpenFileDialog1.FileName
    8. Excel1 = fileinfo.FullName
    9. Me.TextBox1.Text = Replace(fileinfo.Name, ".xls", "")
    10. Me.TextBox1.Text = Replace(fileinfo.Name, ".xlsx", "")
    11. Dim bs As New BindingSource
    12. Using connection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Excel1 & ";Extended Properties=Excel 12.0;")
    13. connection.Open()
    14. Using dataadapter = New OleDbDataAdapter("Select posnr as Pos, Anz as Stk, KZ, BEZ as Bezeichnung, Kennzahl as F, AG, A, B, C, D, E, F as F1, L, W, R, G, H, M, N, X, Y, Ra1Vt as 'Q1/V1' , Ra2Vt as 'Q2/V2', Ra3Vt as 'Q3/V3', round(IsoOf,2) as Isolierung, IsoArt, MA as Material, MS as 'MatS mm', Bem, round([OF],2) as OF1, 0 as gr From [Tabelle1$] where kz like '%' order by posnr", connection)
    15. tbl = New DataTable("Tabelle1")
    16. dataadapter.Fill(tbl)
    17. DataGridView1.DataSource = tbl
    18. End Using
    19. End Using
    20. End If
    21. For Each row As DataGridViewRow In DataGridView1.Rows
    22. If row.Cells(2).Value = "L" Then
    23. 'MsgBox("L")
    24. row.Cells(30).Value = Math.Max(row.Cells(6).Value, row.Cells(7).Value) * 1
    25. row.Cells(30).Value = Int32.Parse(row.Cells(30).Value)
    26. End If
    27. If row.Cells(2).Value = "TA" Or row.Cells(2).Value = "TG" Then
    28. row.Cells(30).Value = grosste_zahl(row.Cells(6).Value, row.Cells(7).Value, row.Cells(8).Value, row.Cells(16).Value, 0) * 1
    29. row.Cells(30).Value = Int32.Parse(row.Cells(30).Value)
    30. End If
    31. Next
    32. DataGridView1.EndEdit()
    33. Dim dv As DataView = DirectCast(DataGridView1.DataSource, DataTable).DefaultView
    34. Dim xls_Appl As Excel.Application
    35. Dim xls_Mappe As Excel.Workbook
    36. zeilenzähler = 4
    37. summenübersicht = 0
    38. summenübersichtiso = 0
    39. If CheckBox1.Checked = False Then
    40. Me.TextBox2.Focus()
    41. dv.RowFilter = "gr < 500"
    42. dv.Sort = "Pos ASC"
    43. DataGridView1.DataSource = dv
    44. End If
    45. end sub
    46. Public Function grosste_zahl(zahl1, zahl2, zahl3, Zahl4, zahl5)
    47. Dim gr As Int32
    48. Dim gr1 As Int32
    49. Dim gr2 As Int32
    50. Dim gr3 As Int32
    51. Dim gr4 As Int32
    52. If zahl1 > zahl2 Then
    53. gr = zahl1
    54. Else
    55. gr = zahl2
    56. End If
    57. If Not IsDBNull(zahl3) Then
    58. If zahl3 > gr And Not IsDBNull(zahl3) Then
    59. gr = zahl3
    60. Else
    61. gr = gr
    62. End If
    63. End If
    64. If Zahl4 > gr And Not IsDBNull(Zahl4) Then
    65. gr = Zahl4
    66. Else
    67. gr = gr
    68. End If
    69. If zahl5 > gr And Not IsDBNull(zahl5) Then
    70. gr = zahl5
    71. Else
    72. gr = gr
    73. End If
    74. grosste_zahl = gr
    75. End Function
    Ok, ich könnte jetzt anfangen, den ganzen Code zu optimieren und Verbesserungs(vor)schläge zu verteilen, führt aber nicht zum Ziel. Daher nochmal meine Anfrage anders formuliert: Erstell doch mal bitte eine Excel-Tabelle, in der ausschließlich die Werte der gr-Spalte sind und importier und sortier die. Es geht doch nur darum, dass Dein DataView-Filter für die Sortierung der gr-Spalte nicht so richtig will. Das ganze andere Gesums (posnr as Pos, Anz as Stk, KZ, BEZ as Bezeichnung, Kennzahl as F, AG, A, B, C, D, E, F as F1, L, W, R, G, H, M, N, X, Y, Ra1Vt as 'Q1/V1' , Ra2Vt as 'Q2/V2', Ra3Vt as 'Q3/V3', round(IsoOf,2) as Isolierung, IsoArt, MA as Material, MS as 'MatS mm', Bem, round([OF],2) as OF1) ist doch für das Problem irrelevant, oder? So sieht es zumindest aus. Also: eine Excel-Tabelle mit nur einer Spalte, in der die gr-Werte drin sind. Die ganze For-Each-Schleife (For Each row As DataGridViewRow In DataGridView1.Rows) weglassen, sofern sie nicht absolut notwendig zur Reproduktion des Problems ist. Es geht darum, dass wir ein Problem nachbauen können/müssen. Je weniger Rauschinformationen (unnötiger Code zur Problemreproduktion) da sind, desto einfacher geht das.
    Dieser Beitrag wurde bereits 5 mal editiert, zuletzt von „VaporiZed“, mal wieder aus Grammatikgründen.

    Aufgrund spontaner Selbsteintrübung sind all meine Glaskugeln beim Hersteller. Lasst mich daher bitte nicht den Spekulatiusbackmodus wechseln.