0
votes

I am working on a windows form application using VB.net. I have populated a Datagridview1( dataset1.existingtable is the datatable). Now i wish to get distinct values from one column of its datatable and then populate another Datagridview2(dataset2.uniquerecords is the datatable).

PROBLEM: Not able to refresh data in Datagridview2 using design mode. However i am able to refresh data when dynamically creating a datatable at runtime. The below sub is called after an event after my form has loaded completely.

The below code does NOT work

Private Sub loaddistinctrecords()

    uniquerecords = existingtable.DefaultView.ToTable(True, "column_name")       
    Datagridview2.Refresh()
End Sub

The below code works

Private Sub loaddistinctrecords()

    Dim newuniquerecords As New DataTable()
    newuniquerecords = existingtable.DefaultView.ToTable(True, "column_name")
    Datagridview2.DataSource = newuniquerecords.DefaultView

End Sub
1

1 Answers

0
votes

well it looks like one cannot directly assign a datatable to another datatable and expect the datagridview to update automatically if the datatable was created from design mode.

What one can do is simply clear the existing records and then merge records from the source datatable.

Private Sub loaddistinctrecords()

  uniquerecords.Clear()
  uniquerecords.Merge(existingtable.DefaultView.ToTable(True, "table_name"))

End Sub