Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Getting Columns from LINQ List
Message
From
03/07/2011 15:19:20
 
 
To
All
General information
Forum:
ASP.NET
Category:
Other
Title:
Getting Columns from LINQ List
Miscellaneous
Thread ID:
01517212
Message ID:
01517212
Views:
90
Hello Everybody.

(I am using VB.net in VS2010)

I got stuck trying to convert a list created with LINQ for a gridview to a datatable.

Here is what i need to do:

I've created a filtered list with LINQ to populate a gridview. At a later point in time the user should be able to export the data from the filtered list. I can not go through the gridview because in I am not showing all the fields collected. So ineed to loop through the DataSource from the gridview.

since i want to allow the user to pick which fields they want to export this has to be dynamic. I have not found a way to get a column count or collection from the list. So i thought i convert the list to a datatable. I found a routine on the web but i am getting the error "Type T is not defined" which obviously is the "iList (of T)".

I know i am missing something obvious.

Here is what I have:
(this populates the gridview i left a lot code out to simplify)
        Dim db = New DataClassesDataContext()
        Dim result_0 =
                From col_Row In db.Masters, z In db.DocumentTypes
                Select
                    ListCol =
                        VendorID = col_Row.VendorID,
                        Vendor_Name = col_Row.Vendor_Name,
...
        If String.IsNullOrWhiteSpace(Me.Project.Text) = False And Me.Project.Text <> "0000" Then
            result_0 = result_0.Where(Function(f) f.Project_ID.ToLower = Project.Text.ToLower)
        End If
...
        Dim TheList = result_0.ToList()

        ' the following line filters results based on user permissions
        Dim result_Match = (From x In TheList Where x.SecurityCode.IncludesAnyOf(UserPermissions) Select x).ToList()

        ' populate the gui items
        If result_Match.Count() > 0 Then
            MyGridView1.DataSourceID = ""
            MyGridView1.DataSource = result_Match
            'MyGridView1.DataKeyNames = "Doc_ID,DocLocation"
            MyGridView1.DataBind()
            MyGridView1.SelectedIndex = 0
            Dim args As New GridViewRowClickedEventArgs(MyGridView1.Rows(0))
            Me.Doc_Count.Text = "Your Search has returned " & result_Match.Count().ToString & " documents."
            GridView1_RowClicked(MyGridView1, args)
        endif
The following is where I am trying to collect the data
...   
         Dim NewDT As DataTable = ToDataTable(Me.MyGridView1.DataSource)
            ' 
            Dim i As Integer = 0
            Do While (i < NewDT.Rows.Count)
                ' build exported data file
                For Each li In NewDT.Rows(i).ItemArray
                    DataLine.Add(NewDT.Rows(i).Item(li.value).ToString.Trim)
                Next
                strData += DataLine.ToString + vbCrLf
                '
                i = (i + 1)
            Loop

...

    Public Shared Function ToDataTable(ByVal data As IList(Of T)) As DataTable
        Dim props As PropertyDescriptorCollection = TypeDescriptor.GetProperties(GetType(T))
        Dim table As DataTable = New DataTable
        Dim n As Integer = 0
        Do While (n < props.Count)
            Dim prop As PropertyDescriptor = props(n)
            table.Columns.Add(prop.Name, prop.PropertyType)
            n = (n + 1)
        Loop
        Dim values() As Object = New Object((props.Count) - 1) {}
        For Each item As T In data
            Dim x As Integer = 0
            Do While (x < values.Length)
                values(x) = props(x).GetValue(item)
                x = (x + 1)
            Loop
            table.Rows.Add(values)
        Next
        Return table
    End Function
Thank you for all your help!
Next
Reply
Map
View

Click here to load this message in the networking platform