Results 1 to 4 of 4

Thread: Problems filtering string/double in datagridview

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2005
    Posts
    48

    Problems filtering string/double in datagridview

    Hi ,

    I have a problem filteting a datagrid that read his information from a txt file, the grid has this.

    Pendiente Longitud Parametro Vertice
    0 0 0 0
    0,733333 120 20007,04 9365,221
    6,73122 50 9292,37 9562,563
    12,111977 62,627 1250 9661,794
    -37,99 76,629 2100 9796,755
    -1,5 58,4 1600 10164,71
    -38 190 2500 10603,699
    38 127,75 3500 10981,034
    1,5 57,5 5000 11247,151
    -10 192 4000 11784,128
    38 127,75 3500 12152,33
    1,5 301,486 8305,4 12489,698
    37,8 72,6 2000 12982,12
    1,5 67,5 15000 13232,408
    6 96 4000 13455,722
    30 57 2000 14328,915
    1,5 0 0 0

    This grid all rows, are DataGridViewCellStyle ,without format

    i'm trying it load datagrid to a datatable, filter and upload again datatable to grid (not surre if it's the best way)

    Private Sub Button3_Click_1(sender As Object, e As EventArgs) Handles Button3.Click

    Dim dset As New DataSet
    For i As Integer = 0 To DataGridView1.ColumnCount - 1
    dset.Tables(0).Columns.Add(DataGridView1.Columns(i).HeaderText)
    Next

    Dim dr1 As DataRow
    For i As Integer = 0 To DataGridView1.RowCount - 1
    dr1 = dset.Tables(0).NewRow
    For j As Integer = 0 To DataGridView1.Columns.Count - 1
    dr1(j) = DataGridView1.Rows(i).Cells(j).Value
    Next
    dset.Tables(0).Rows.Add(dr1)
    Next

    Dim dv As New DataView
    Dim filtro As String

    dv = dset.Tables(0).DefaultView

    filtro = "Longitud<" & "'" & TextBox1.Text.ToString & "'"
    dv.RowFilter = filtro
    DataGridView1.Rows.Clear()
    DataGridView1.Columns.Clear()
    DataGridView1.Refresh()
    DataGridView1.DataSource = dv

    end sub

    textboxt1.text there is a 50 ,after click button 3 ,grid show me this

    Pendiente Longitud Parametro Vertice
    0,733333 120 20007,04 9365,221
    -38 190 2500 10603,699
    38 127,75 3500 10981,034
    -10 192 4000 11784,128
    38 127,75 3500 12152,33
    1,5 301,486 8305,4 12489,698
    1,5 0 0 0


    Wha'ts wrong? ,the filter take some more than Longitud>50 and left some <50

    I don't know how to fix it...

    Thanks for all

  2. #2
    Addicted Member thetimmer's Avatar
    Join Date
    Jan 2014
    Location
    Plano, Texas
    Posts
    243

    Re: Problems filtering string/double in datagridview

    Please remember to wrap your code in tags

    Hi ,

    I have a problem filteting a datagrid that read his information from a txt file, the grid has this.

    Pendiente Longitud Parametro Vertice
    0 0 0 0
    0,733333 120 20007,04 9365,221
    6,73122 50 9292,37 9562,563
    12,111977 62,627 1250 9661,794
    -37,99 76,629 2100 9796,755
    -1,5 58,4 1600 10164,71
    -38 190 2500 10603,699
    38 127,75 3500 10981,034
    1,5 57,5 5000 11247,151
    -10 192 4000 11784,128
    38 127,75 3500 12152,33
    1,5 301,486 8305,4 12489,698
    37,8 72,6 2000 12982,12
    1,5 67,5 15000 13232,408
    6 96 4000 13455,722
    30 57 2000 14328,915
    1,5 0 0 0

    This grid all rows, are DataGridViewCellStyle ,without format

    i'm trying it load datagrid to a datatable, filter and upload again datatable to grid (not surre if it's the best way)

    Code:
    Private Sub Button3_Click_1(sender As Object, e As EventArgs) Handles Button3.Click
    
    Dim dset As New DataSet
    For i As Integer = 0 To DataGridView1.ColumnCount - 1
    dset.Tables(0).Columns.Add(DataGridView1.Columns(i).HeaderText)
    Next
    
    Dim dr1 As DataRow
    For i As Integer = 0 To DataGridView1.RowCount - 1
    dr1 = dset.Tables(0).NewRow
    For j As Integer = 0 To DataGridView1.Columns.Count - 1
    dr1(j) = DataGridView1.Rows(i).Cells(j).Value
    Next
    dset.Tables(0).Rows.Add(dr1)
    Next
    
    Dim dv As New DataView
    Dim filtro As String
    
    dv = dset.Tables(0).DefaultView
    
    filtro = "Longitud<" & "'" & TextBox1.Text.ToString & "'"
    dv.RowFilter = filtro
    DataGridView1.Rows.Clear()
    DataGridView1.Columns.Clear()
    DataGridView1.Refresh()
    DataGridView1.DataSource = dv
    
    end sub
    textboxt1.text there is a 50 ,after click button 3 ,grid show me this

    Pendiente Longitud Parametro Vertice
    0,733333 120 20007,04 9365,221
    -38 190 2500 10603,699
    38 127,75 3500 10981,034
    -10 192 4000 11784,128
    38 127,75 3500 12152,33
    1,5 301,486 8305,4 12489,698
    1,5 0 0 0


    Wha'ts wrong? ,the filter take some more than Longitud>50 and left some <50

    I don't know how to fix it...

    Thanks for all
    Last edited by thetimmer; Mar 19th, 2014 at 01:08 PM.
    _____________
    Tim

    If anyone's answer has helped you, please show your appreciation by rating that answer.
    When you get a solution to your issue remember to mark the thread Resolved.


    reference links

  3. #3
    Addicted Member thetimmer's Avatar
    Join Date
    Jan 2014
    Location
    Plano, Texas
    Posts
    243

    Re: Problems filtering string/double in datagridview

    The filter is seeing your values like 301,486 as the number 301,486 ( three hundred and one thousand four hundred and eighty six ). I think you'll want to do a replace on your commas with periods in the filter string...

    Code:
    filtro = "Longitud<" & "'" & TextBox1.Text.ToString.Replace(",", ".") & "'"
    Last edited by thetimmer; Mar 19th, 2014 at 01:12 PM.
    _____________
    Tim

    If anyone's answer has helped you, please show your appreciation by rating that answer.
    When you get a solution to your issue remember to mark the thread Resolved.


    reference links

  4. #4
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Problems filtering string/double in datagridview

    It will be easier to define a typed DataTable and use that table as the DataSource for the DataGridView. This way the DataBindings created when you assign the DataSource will handle everything for you and the values you type in the DataGridView will be stored in the DataTable directly.

    You could create a DataSet (Project Menu->Add New Item->DataSet) and then define a typed DataTable in the designer, but for this example I created the minimum need definition in the code instead.

    I misinterpreted your post originally and thought you were loading the data from a text file, so there is code include to do that if you decide you want to use it.

    VB.Net Code:
    1. Public Class Form1
    2.  
    3.    Private MyData As New MyDataTable
    4.  
    5.    ' define a DataTable with the needed columns and datatypes
    6.    Private Class MyDataTable
    7.       Inherits DataTable
    8.       Public Sub New()
    9.          With Columns
    10.             .Add("Pendiente", GetType(Double))
    11.             .Add("Longitud", GetType(Double))
    12.             .Add("Parametro", GetType(Double))
    13.             .Add("Vertice", GetType(Double))
    14.          End With
    15.       End Sub
    16.    End Class
    17.  
    18.    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    19.       ' Do not include the next line in your code.
    20.       ' I needed to change the culture to handle your use of a comma as the decimal mark
    21.       My.Application.ChangeCulture("de-DE")
    22.  
    23.       'LoadData() ' this method would load the data from a text file if you need that.
    24.       DataGridView1.DataSource = MyData.DefaultView
    25.    End Sub
    26.  
    27.    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    28.       Dim Longitud As Double
    29.       If Double.TryParse(TextBox1.Text, Longitud) Then
    30.          MyData.DefaultView.RowFilter = "[Longitud] < " & TextBox1.Text
    31.       Else
    32.          MyData.DefaultView.RowFilter = String.Empty
    33.       End If
    34.    End Sub
    35.  
    36.    Private Sub DataGridView1_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles DataGridView1.DataError
    37.       MsgBox("Please enter a valid number or nothing")
    38.    End Sub
    39.    Private Sub LoadData()
    40.       Dim sr As New IO.StreamReader("textfile1.txt")
    41.       Dim newRow As DataRow
    42.       Dim data() As String
    43.  
    44.       Do While Not sr.EndOfStream
    45.          data = sr.ReadLine().Split()
    46.          If data.Length = MyData.Columns.Count Then
    47.             newRow = MyData.NewRow ' get a new typed datarow
    48.             Try
    49.                ' let the datarow handle the parsing
    50.                ' an exception will be raised if the string can not be coverted into the proper type
    51.                newRow.ItemArray = data
    52.                MyData.Rows.Add(newRow)
    53.             Catch ex As Exception
    54.                ' log invalid data
    55.             End Try
    56.          Else
    57.             ' log invalid data
    58.          End If
    59.       Loop
    60.    End Sub
    61.  
    62. End Class

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width