VB.NET - Sync DataTable and DataGridView - Asked By Brad Feilmeier on 05-Aug-10 10:51 AM

Hello,

I'm working on a bioinformatics application that requires parsing text files into datasets that are then bound to a datagridview to make the data easier to work with.  The problem I have is that the data in the datatable is not in sync with the data in the datagridview after a column sort event is performed.  There must be an easy way to bind the datagridview data to the datatable data.  Here is the simple code that I'm using:

        dgv.AutoGenerateColumns = True
        dgv.DataSource = DS  'DS is the dataset that holds many tables, including the FASTA_DATA table
        dgv.DataMember = DS.Tables("FASTA_DATA").TableName '"FASTA_DATA"

The goal is to perform a sort and have the data updated in the datagridview also updated in the datatable.  I should note that I'm not a professional programmer, but a biologist who knows just enough VB to be dangerous, so I greatly appreciate your help!

Brad

Peter Bromberg replied to Brad Feilmeier on 05-Aug-10 10:59 AM
Private Sub SortByTwoColumns()
    ' Get the DefaultViewManager of a DataTable.
    Dim view As DataView = DataTable1.DefaultView

    ' By default, the first column sorted ascending.
    view.Sort = "State, ZipCode DESC"

MyDataGrid.DataSource=view;
MyDataGrid.DataBind()
End Sub


The view will now be synchronized. So use the DataView instead of the DataTable.
Brad Feilmeier replied to Peter Bromberg on 05-Aug-10 11:16 AM
Hi Peter,

Thanks for the answer.  I'm probably doing something stupid here, but I don't know what.  Here's my code:

    Private Sub Sync()
        Dim FastaTable As DataTable = DS.Tables("FASTA_DATA")
        Dim DV As DataView = FastaTable.DefaultView
        dgv.DataSource = DV
        dgv.DataBind()  
    End Sub

dgv is my DataGridView.  For some reason, I get the "DataBind is not a member of .....".  Is this because I'm using .NET 3.5 or am I missing a reference?

Thanks again!  I greatly appreciate it.

Brad
Peter Bromberg replied to Brad Feilmeier on 05-Aug-10 11:21 AM
If dgv is a live reference to your DataGridView (at least in ASP.NET) it should have a DataBind() method.  In Windows Forms, all you need to do is set the DataSource, the grid will bind automatically.
Brad Feilmeier replied to Peter Bromberg on 05-Aug-10 11:31 AM
Perfect!  Thank you so much.  I appreciate the help.

Brad
Peter Bromberg replied to Brad Feilmeier on 05-Aug-10 11:39 AM
Happy to help.