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
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
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(",", ".") & "'"
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:
Public Class Form1
Private MyData As New MyDataTable
' define a DataTable with the needed columns and datatypes
Private Class MyDataTable
Inherits DataTable
Public Sub New()
With Columns
.Add("Pendiente", GetType(Double))
.Add("Longitud", GetType(Double))
.Add("Parametro", GetType(Double))
.Add("Vertice", GetType(Double))
End With
End Sub
End Class
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' Do not include the next line in your code.
' I needed to change the culture to handle your use of a comma as the decimal mark
My.Application.ChangeCulture("de-DE")
'LoadData() ' this method would load the data from a text file if you need that.
DataGridView1.DataSource = MyData.DefaultView
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Dim Longitud As Double
If Double.TryParse(TextBox1.Text, Longitud) Then
MyData.DefaultView.RowFilter = "[Longitud] < " & TextBox1.Text
Else
MyData.DefaultView.RowFilter = String.Empty
End If
End Sub
Private Sub DataGridView1_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles DataGridView1.DataError
MsgBox("Please enter a valid number or nothing")
End Sub
Private Sub LoadData()
Dim sr As New IO.StreamReader("textfile1.txt")
Dim newRow As DataRow
Dim data() As String
Do While Not sr.EndOfStream
data = sr.ReadLine().Split()
If data.Length = MyData.Columns.Count Then
newRow = MyData.NewRow ' get a new typed datarow
Try
' let the datarow handle the parsing
' an exception will be raised if the string can not be coverted into the proper type
newRow.ItemArray = data
MyData.Rows.Add(newRow)
Catch ex As Exception
' log invalid data
End Try
Else
' log invalid data
End If
Loop
End Sub
End Class