Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 972
  • Last Modified:

Convert Datatable to Ilist

I have a RadGrid project that uses drag and drop with IList Data handling the data transfers.

My problem is that I need to populate one of the grids with data FIRST.  So...

My attached code will populate my grid using a datatable.

What I NEED to do is convert the Datatable to an IList named ShippedOrdersStore and use that as the datasource.
Protected Sub grdCollateral_NeedDataSource(ByVal source As Object, ByVal e As GridNeedDataSourceEventArgs)
        Dim objConn1 As SqlConnection
        objConn1 = New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("ConnPortal"))
        Dim oCom1 As SqlCommand
        oCom1 = New SqlCommand
        oCom1.Connection = objConn1
        oCom1.CommandText = "sp_cfa_TestCollateral"
        oCom1.CommandType = CommandType.StoredProcedure
        Dim dA As SqlDataAdapter = New SqlDataAdapter(oCom1)
        Dim myDataTable As DataTable = New DataTable()
        objConn1.Open()
        Try
            dA.Fill(myDataTable)
        Finally
            objConn1.Close()
        End Try
        grdCollateral.DataSource = myDataTable.DefaultView
        grdCollateral.GroupingSettings.CaseSensitive = False
        objConn1.Dispose()
        oCom1.Dispose()
        oCom1 = Nothing
    End Sub

Open in new window

0
lrbrister
Asked:
lrbrister
  • 2
1 Solution
 
kovilpattiBaluCommented:
hi,
here is ther code

public static IList<T> ConvertTo<T>(DataTable table)
    {
        if (table == null)
            return null;
 
        List<DataRow> rows = new List<DataRow>();
 
        foreach (DataRow row in table.Rows)
            rows.Add(row);
 
        return ConvertTo<T>(rows);
    }
0
 
CodeCruiserCommented:
If you are still interested, here is the VB.NET code

Replace

        Try
            dA.Fill(myDataTable)
        Finally
            objConn1.Close()
        End Try
        grdCollateral.DataSource = myDataTable.DefaultView


with
        Dim MyList as List(of DataRow)
        Try
            dA.Fill(myDataTable)
            For Each dRow in myDataTable.Rows
                  MyList.Add(dRow)
            Next
        Finally
            objConn1.Close()
        End Try
        grdCollateral.DataSource = MyList
0
 
lrbristerAuthor Commented:
Thanks
0
 
CodeCruiserCommented:
Glad to help :-)
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now