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.
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 :