user13644212
user13644212

Reputation:

Datagridview filters in VB.net

I'm trying to filter a datagridview containing flights, by chosing a period of time, a route or a budget.

The code seems to work fine for the route (RotaID) but not for the dates (DataHoraPartida) nor the budget (PrecoBase).

This is the code

Public Class FormProcurarViagem
    Private ListaViagens As New Viagens
    Private ListaRotas As New Rotas

    Private DataSetTemp As New DataSet()

    Private DataSource2Grid As New BindingSource()


    Public Event EnviaDadosViagem(ByVal NovaReservaViagem As ReservaViagem)

        Private Sub FormReservas_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ListaViagens = LerXML_Viagens()
        Me.ViagensBindingSource.DataSource = ListaViagens

        FillGrid()


        ListaRotas = LerXML_Rotas()

        With ComboBox1
            .DisplayMember = "RotaID"
            .ValueMember = "RotaID"
            .DataSource = ListaRotas
            .DataBindings.Add("SelectedValue", ViagensBindingSource, "RotaID", DataSourceUpdateMode.OnPropertyChanged)
        End With

        With ComboBox2
            Dim i As Integer
            Dim j As Integer

            For j = 0 To 10
                i = i + 50
                ComboBox2.Items.Add(i)
            Next j


        End With

        DateTimePicker1.Format = DateTimePickerFormat.Custom
        DateTimePicker1.CustomFormat = "yyyy/MM/dd"
        DateTimePicker2.Format = DateTimePickerFormat.Custom
        DateTimePicker2.CustomFormat = "yyyy/MM/dd"

    End Sub

    Private Sub FillGrid()

          DataSetTemp.ReadXml("Viagens.xml")


        Dim tables As DataTableCollection = DataSetTemp.Tables

        Dim view1 As New DataView(tables(0))

        ViagensDataGridView.AutoGenerateColumns = True

        Me.Controls.Add(ViagensDataGridView)



        ' Create a BindingSource and set its DataSource property to the DataView.

        DataSource2Grid.DataSource = view1


        ' Set the data source for the DataGridView.

        ViagensDataGridView.DataSource = DataSource2Grid

    End Sub


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        DataSource2Grid.Filter = String.Format("DataHoraPartida >= '{0:yyyy/MM/dd}' And DataHoraPartida <= '{1:yyyy/MM/dd}'", DateTimePicker1.Value.Date, DateTimePicker2.Value.Date)

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        DataSource2Grid.Filter = "PrecoBase <=" & Me.ComboBox2.Text
    End Sub


    Private Sub ComboBox1_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles ComboBox1.SelectionChangeCommitted

        DataSource2Grid.Filter = "RotaID like '%" & Me.ComboBox1.Text & "%'"
    End Sub


End Class

In the data source the date format is yyyy/MM/dd HH:mm and the PrecoBase format is double, I don't know if that is what is causing the problem.

Upvotes: 0

Views: 977

Answers (1)

jmcilhinney
jmcilhinney

Reputation: 54487

If the data in your data source is not type DateTime then it should be, so you should first take steps to remedy that. You can then apply whatever formatting you want in the grid by setting the .DefaultCellStyle.Format property of the column.

Once you actually have data of the correct type, you can filter it properly, as per the documentation. Filtering actual Dates would look like this:

DataSource2Grid.Filter = String.Format("DataHoraPartida >= #{0:M/dd/yyyy}#' AND DataHoraPartida <= #{1:M/dd/yyyy}#", DateTimePicker1.Value, DateTimePicker2.Value)

DateTime literals are ALWAYS delimited with # characters and they are ALWAYS formatted using US date format, i.e. M/dd/yyyy. There's also no point specifying the Date property of the two values because the format already drops the time. I'd also recommend using string interpolation over String.Format in most cases if you're using VB 2015 (I think, or it may be 2017) or later:

DataSource2Grid.Filter = $"DataHoraPartida >= #{DateTimePicker1.Value:M/dd/yyyy}#' AND DataHoraPartida <= #{DateTimePicker2.Value:M/dd/yyyy}#"

Upvotes: 1

Related Questions