Filtering Datagridview String and Integer

batserra

Member
Joined
Oct 6, 2008
Messages
6
Programming Experience
Beginner
Hi,

I have a grid from a txt file that contains:

VB.NET:
A                 B                 C                      D
0.733333      120.000        20007.047          9365.221          
6.731220       50.000        9292.373            9562.563          
12.111977       62.627       1250.000            9661.794

I want to filter by colum b from more than a number that is in a txtbox
But i don't know how.
The problem is that the grid is a string and not intenger?

I think that the best solution is working with a dataset ,pass all grid to a dataset and work from there

   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


And after:

Dim dv As New DataView(dset.Tables(0))
dv.RowFilter = "CONVERT(B, 'System.Int32')" > textbox1.text


But it's not working

Can anybody help me?

Thanks for all ,and sorry for my english :)
 
Last edited by a moderator:
Hi

I notice that you are using .NET2 but hopefully this example using .NET4 will help you to get where you need to be.

The first thing to do is to get your data into a DataTable. Once done, you can then set the Datasource of a BindingSource to the DataTable, set the DataSource of the DataGridView to the BindingSource and then use the Filter property of the BindingSource to achieve your results.

Have a play with these steps:-

Setup the DataTable:-
Dim myDT As New DataTable
Dim myBS As New BindingSource
 
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  SetUpDataTable()
End Sub
 
Private Sub SetUpDataTable()
  For Counter As Integer = 1 To 4
    myDT.Columns.Add(New DataColumn("Col" & Counter, GetType(Double)))
  Next
End Sub


Read your Data and set the Datasource properties:-
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
  Dim someDataRecords() As String = IO.File.ReadAllLines("c:\temp\YourData.txt")
 
  For Each dataRecord As String In someDataRecords
    myDT.Rows.Add(dataRecord.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries))
  Next
  myBS.DataSource = myDT
  DataGridView1.DataSource = myBS
End Sub


Filter Using a Textbox:-
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
  myBS.Filter = "Col2 >=" & CDbl(TextBox1.Text)
End Sub


In all fairness, the above code could do with some additional validation statements but in principal this will do the job fine.

Hope that helps.

Cheers,

Ian
 
Thanks to tray to help me ,but i still have the same problem

Dim source1 As New BindingSource()
Dim view1 As New DataView(dset.Tables(0))
source1.DataSource = view1

source1.Filter = "longitud>=" & CDbl(TextBox1.Text)


and the error

Can not perform operetion '>=' on System.String and System.Int32.
 
Obviously the issue is comparing incompatible data types so make sure that you use compatible data types in your comparisons. If you want to filter by the Integer column then use an Integer value. If you want to filter by the String column then use a String value. You have to wrap the value in single quotes to denote it as a String.
 
Thanks to tray to help me ,but i still have the same problem

Dim source1 As New BindingSource()
Dim view1 As New DataView(dset.Tables(0))
source1.DataSource = view1

source1.Filter = "longitud>=" & CDbl(TextBox1.Text)


and the error

Can not perform operetion '>=' on System.String and System.Int32.


thanks to help me, now i have the same error but i have some changes...

First

Dim pendiente,longitud,parametro,... As Double

While Not Archivo.EndOfStream
stringReader = Archivo.ReadLine()
pendiente = 0
longitud = 0
parametro = 0
...

pendiente = Val(stringReader.Substring(7, 11).Trim)
longitud = Val(stringReader.Substring(20, 11).Trim)
parametro = Val(stringReader.Substring(33, 11).Trim)
...

DataGridView1.Rows.Add(pendiente, longitud, parametro..)

End While

-----

Now in datagridview are double rows

function grid to table and filter
Dim dset As New DataSet
dset.Tables.Add()
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 source1 As New BindingSource()
        Dim view1 As New DataView(dset.Tables(0))
        source1.DataSource = view1
     
        source1.Filter = "Longitud > '100' "
        DataGridView1.DataSource = source1.DataSource


Now threre is no error with dv.rowfilter= "Longitud > '100'" but datagridview1 the is not any rows ,it's a white grid

thanks to help me
 
Last edited by a moderator:
Back
Top