Answered Linq to Dataset query as datagridview datasource issue

dsk96m

Well-known member
Joined
Jan 11, 2013
Messages
173
Programming Experience
1-3
So I have a query with results that I want to put into a datagridview. I have it working except I get an error on the last line.
VB.NET:
    Private Sub frm_loadcards_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.ModetypeTableAdapter.Fill(Me.FlightTestApp2DataSet.modetype)
        Me.FlightcardTableAdapter.Fill(Me.FlightTestApp2DataSet.flightcard)

        Dim cards = FlightTestApp2DataSet.Tables("flightcard")
        Dim mode = FlightTestApp2DataSet.Tables("modetype")
        Dim query = From c In cards.AsEnumerable Join m In mode.AsEnumerable
                  On c.Field(Of Integer)("mode_id") Equals m.Field(Of Integer)("mode_id")
                  Select New With
                         {
                             .flightcardid = c.Field(Of Integer)("flightcard_id"),
                             .flightcardname = c.Field(Of String)("name"),
                             .flightcardrev = c.Field(Of Integer)("revision"),
                             .flightcardmode = m.Field(Of String)("abbreviation")
                         }

        Dim results = query.ToList

        Me.DataGridView1.DataSource = results
        Me.DataGridView1.Columns(0).Visible = False
        Me.DataGridView1.Columns(1).HeaderText = "Name"
        Me.DataGridView1.Columns(2).HeaderText = "Rev"
        Me.DataGridView1.Columns(3).HeaderText = "Mode"
        Me.DataGridView1.Sort(Me.DataGridView1.Columns(3), System.ComponentModel.ListSortDirection.Ascending)
    End Sub

The error i get is DataGridView control must be bound to an IBindingList object to be sorted.

I am not sure what to do. Maybe the tolist isnt the right way to go. Can I create a datatable from the results? I have searched and saw the copytodatatable, but that doesnt work because of the of anonymous type. Please help.
 
Last edited:
You could sort the list of that anonymous type before binding it: Select item = New With {...} Order By item.flightcardmode
 
I went a little different route. I wanted to sort by several columns. I guess I could do it that way too. That would probably be more correct, lol.

VB.NET:
    Private Sub frm_loadcards_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.ModetypeTableAdapter.Fill(Me.FlightTestApp2DataSet.modetype)
        Me.FlightcardTableAdapter.Fill(Me.FlightTestApp2DataSet.flightcard)

        Dim dt As New DataTable
        dt.Columns.Add("flightcardid", GetType(Integer))
        dt.Columns.Add("flightcardname", GetType(String))
        dt.Columns.Add("flightcardrev", GetType(Integer))
        dt.Columns.Add("flightcardmode", GetType(String))

        Dim cards = FlightTestApp2DataSet.Tables("flightcard")
        Dim mode = FlightTestApp2DataSet.Tables("modetype")
        Dim query = From c In cards.AsEnumerable Join m In mode.AsEnumerable
                  On c.Field(Of Integer)("mode_id") Equals m.Field(Of Integer)("mode_id")
                  Select New With
                         {
                             .flightcardid = c.Field(Of Integer)("flightcard_id"),
                             .flightcardname = c.Field(Of String)("name"),
                             .flightcardrev = c.Field(Of Integer)("revision"),
                             .flightcardmode = m.Field(Of String)("abbreviation")
                         }

        For Each x In query
            dt.Rows.Add(x.flightcardid, x.flightcardname, x.flightcardrev, x.flightcardmode)
        Next

        Me.DataGridView1.DataSource = dt
        Me.DataGridView1.Columns(0).Visible = False
        Me.DataGridView1.Columns(1).HeaderText = "Name"
        Me.DataGridView1.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
        Me.DataGridView1.Columns(2).HeaderText = "Rev"
        Me.DataGridView1.Columns(3).HeaderText = "Mode"
        'Me.DataGridView1.Sort(Me.DataGridView1.Columns(3), System.ComponentModel.ListSortDirection.Ascending)

        Dim view As DataView = dt.DefaultView
        view.Sort = "flightcardmode ASC,flightcardname ASC,flightcardrev ASC"
        Me.DataGridView1.DataSource = view
    End Sub
 
Back
Top