My Technical Notes

Monday, 11 August 2014

F#: Converting Rows Back to a DataTable

Because the DataGridView does not display an array of DataRows, but can deal with a DataTable, the easiest course of action is to convert our array of DataRows into a DataTable and then pass it into the DataGridView. Below is code which does this:

let backToDt rows = 
    let rows = rows |> Seq.cast<DataRow> |> Seq.toList
    match rows with
    | [] -> new DataTable()
    | x::xs -> 
        let dt = x.Table.Clone()
        for row in rows do
            dt.Rows.Add(row.ItemArray) |> ignore

No comments: