将列表转换为 table 并绑定到 gridview

convert list to table and bind to gridview

我有一个包含名称和创建日期的字段的列表项

        Dim files As List(Of ListItem) = New List(Of ListItem)
        For Each filePath As String In filePaths
            files.Add(New ListItem(Path.GetFileName(filePath), filePath))
            files.Add(New ListItem(File.GetCreationTime(filePath), filePath))
        Next
        GridView2.DataSource = files
        GridView2.DataBind()

绑定到边界域

   <asp:BoundField DataField="Text" HeaderText="Report Name" />

第一个 files.add 是 name 第二个是 Creation date 它只显示一列我如何将它们转换为 2 列

   <asp:BoundField DataField="Text" HeaderText="Report Name" />
<asp:BoundField DataField="???" HeaderText="Creation Date" />

您需要在此处使用 DataTable 而不是 ListItem,因为您可能需要绑定两个以上的列。

正在创建数据表

Function GetTable() As DataTable
{
    // Here we create a DataTable with four columns.
     Dim table As New DataTable
     table.Columns.Add("FileName");
     table.Columns.Add("FileCreationTime");
     For Each filePath As String In filePaths
        table.Rows.Add(Path.GetFileName(filePath), File.GetCreationTime(filePath).ToString())
     Next   
    return table;
}

正在将 DataTable 分配给 GridView 数据源

GridView2.DataSource = GetTable()
GridView2.DataBind()

用控件绑定 DataTable 列

<asp:BoundField DataField="FileName" HeaderText="Report Name" />
<asp:BoundField DataField="FileCreationTime" HeaderText="Creation Date" />