JeffO
JeffO

Reputation: 8043

Filter on dataset clears all bound controls

The following code works (rows are filtered by the select expression), but then all the controls in the datarepeater are empty. When set to .DefaultView all records return and all controls have their values.

       Private Sub CheckBox_FilterApplied_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBox_FilterApplied.CheckedChanged
    If CheckBox_FilterApplied.Checked Then
        ' RichTextBox_Notes.DataBindings.Add("Text", dsTransactions.Tables("TransactionHeader"), "Note")
        DataRepeater_Transactions.DataSource = dsTransactions.Tables("TransactionHeader").Select("Applied = 0")

        DataRepeater_Transactions.Refresh()
    Else
        DataRepeater_Transactions.DataSource = dsTransactions.Tables("TransactionHeader").DefaultView

    End If
End Sub

Can't tell what is missing. Refresh is no help.

Upvotes: 2

Views: 829

Answers (2)

Thomas
Thomas

Reputation: 64645

The DefaultView property is typed as a DataView whose IEnumerable enumerates over a DataRowView array which enables you to use the standard binding syntax. However, the Select method returns an array of DataRow objects which cannot be bound in the same way. The simplest solution is to ensure you pass a DataView to the DataSource property.

If CheckBox_FilterApplied.Checked Then
    Dim dt As DataTable = dsTransactions.Tables("TransactionHeader")
    Dim dv As DataView = New DataView(dt, "Applied = 0", "", DataViewRowState.CurrentRows)
    DataRepeater_Transactions.DataSource = dv

Else
    DataRepeater_Transactions.DataSource = dsTransactions.Tables("TransactionHeader")

End If

Also note, that can bind directly to the DataTable and do not need to explicitly use the DefaultView property as it will be used by default.

Upvotes: 1

Anuraj
Anuraj

Reputation: 19598

I think the problem because of the DataSource of the Textbox and Datasource of the DataRepeater.

I modified the code slightly, Please try it. Works for me.

Dim dt As New DataTable
dt.Columns.Add("Col1")
dt.Columns.Add("Col2")
dt.Columns.Add("Col3")


For index = 1 To 10
    Dim dr As DataRow = dt.NewRow()
    dr("Col1") = index.ToString()
    dr("Col2") = index.ToString()
    dr("Col3") = index.ToString()
    dt.Rows.Add(dr)
Next

Dim dv As DataView = New DataView(dt, "Col1 >= 8", "", DataViewRowState.CurrentRows)

TextBox1.DataBindings.Add(New Binding("Text", dv, "Col3"))
DataRepeater1.DataSource = dv

Hope it helps :)

Upvotes: 1

Related Questions