0

I am using vb.net. I have a datagridview to show data. Since the datagridview needs a filter function, I have an underlying data table of datagridview and filter the view of data table:

     PassFilter(m_table,"Price>0")
    Private Sub PassFilter(ByRef dataTable As DataTable, ByVal strFilter As String)     Try
        Dim dataview As New DataView(dataTable)
        dataview.RowFilter = strFilter
        'DGVTable is my datagridview
        DGVTable.DataSource = dataview
End Sub

This works well. But I have troubles in the following code:

      Private Sub UpdateTable()
      Dim row as dataRow=m_table.NewRow
      row("column1")=GetText()
      m_table.Rows.Add(row)
      End Sub

Note that GetText() seems be run on a different thread which returns value after the above codes for the first time.(I am not clear why this happens, but it seems that the second time to call GetText, it can get the value directly) This means that the column1 in m_table is actually empty after the above code for the first time, so datagridview is also empty since its datasource is m_table. I have no choice but to call the above sub again to update datagridview. This is inefficient.

The problem is I want to keep filter simple, then the underlying table seems necessary. But the table does not have invalidate as controls. So table cannot update values. I must call my sub again to update the table.

How can I keep filter simple as well as use datagridview.invalidate to update values so that my sub does not need to be called again? Any help is apprecaited.

1 Answer 1

0

You should not create a new view each time you want to filter your DataGrid with a different string, but rather just update the filter of the current default view. In the same idea of separating concerns, just assign once the default view of your DataTable to your DataGridView. To give an idea that would lead to a code like this :

' SetFilter : Set the current filter of MyDataTable to strFilter
Private Sub SetFilter(ByVal MyDataTable As DataTable, ByVal strFilter As String)
    Dim DefaultViewOfMyDataTable As DataView = MyDataTable.DefaultView
    DefaultViewOfMyDataTable.RowFilter = strFilter
End Sub

' And Somewhere else in the code / or in the xaml
' you put this sub that you should in the new or the loaded() sub of your window.

' InitDataGridView : Assign MyDataTable to MyDGV DataGridView
Private Sub InitDataGridView(MyDGV As DataGridView, MyDataTable As DataTable)
    MyDGV.DataSource = MyDataTable.DefaultView
End Sub
4
  • Thanks for your answer. I wonder why it is not a good idea to create a new view each time. I tried your answer and it filters well just as my previous code. I guess it is more efficient?
    – Summer
    Jan 17, 2012 at 17:53
  • Well it will be more efficient, yes, and more reusable also. Don't do it if you don't need to. By the way, if think i understood your '1 step after' display : refresh is done because you change DataContext, and change in your DataTable is not reflected otherwise. It seems that if you are using a DataView, you should handle all this in a different manner. I haven't time to write code/test but look at this msdn.microsoft.com/en-us/library/70k6287b(VS.71).aspx for example. Jan 17, 2012 at 20:39
  • Thanks for your answer. My problem is really hard to explain clearly.I will leave it this way. But your answer shows me something that I did not know before.
    – Summer
    Jan 17, 2012 at 21:03
  • Yes sometimes bugs can be such a mess... until the light comes out, suddenly, on a sunday morning !! :-) glad i could help. Jan 17, 2012 at 21:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.