I have a DataTable where the columns are generated programmatically at runtime. I then bind this DataTable to a GridView. What I'm wondering is how I can create the GridView to accommodate开发者_JS百科 this, and if it's not possible, how I can output the DataTable into nicely formatted HTML.
The GridView has an AutogenerateColums-property for this purpose. You could also generate the Columns on the fly, for example:
VB.NET
Dim tbl As New DataTable
tbl.Columns.Add("ID", GetType(Int32))
tbl.Columns.Add("Name", GetType(String))
tbl.Columns.Add("Birthday", GetType(Date))
Dim pers As DataRow = tbl.NewRow
pers("ID") = 1
pers("Name") = "Tim"
pers("Birthday") = New Date(1973, 6, 9)
use AutoGenerateColumns to let grid generate the column itself:
Me.GridView1.AutoGenerateColumns = True
Me.GridView1.DataSource = tbl
Me.GridView1.DataBind()
or generate the columns dynamically
For Each col As DataColumn In tbl.Columns
    Dim field As New BoundField
    field.DataField = col.ColumnName
    field.HeaderText = col.ColumnName
    GridView1.Columns.Add(field)
Next
C#
foreach (DataColumn col in dt.Columns)
{    
    BoundField field = new BoundField();
    field.DataField = col.ColumnName;
    field.HeaderText = col.ColumnName;
    GridView1.Columns.Add(field);
}
You should be able to just set the DataTable as the gridview's DataSource. GridView has a AutoGenerateColumns property. Make sure it's set to true or pre-create columns manually yourself before performing the binding.
Just because the other answers didn't cover this, here is how to set up a GridView with custom columns programatically.
    private GridView SetUpGrid()
    {
        GridView GView = new GridView();
        GView .ColumnHeaderToolTip = "MyToolTip";
        GridViewColumn gvc1 = new GridViewColumn();
        gvc1.DisplayMemberBinding = new Binding("Col1Name");
        gvc1.Header = "Column One";
        gvc1.Width = Double.NaN; // Auto-Size
        GView .Columns.Add(gvc1);
        GridViewColumn gvc2 = new GridViewColumn();
        gvc2.DisplayMemberBinding = new Binding("Col2Name");
        gvc2.Header = "Column Two";
        gvc2.Width = Double.NaN;
        GView .Columns.Add(gvc2);
        GridViewColumn gvc3 = new GridViewColumn();
        gvc3.DisplayMemberBinding = new Binding("Col3Name");
        gvc3.Header = "Column Three";
        gvc3.Width = Double.NaN;
        GView .Columns.Add(gvc3);
        GridViewColumn gvc4 = new GridViewColumn();
        gvc4.DisplayMemberBinding = new Binding("Col4Name");
        gvc4.Header = "Column Four";
        gvc4.Width = Double.NaN;
        GView .Columns.Add(gvc4);
        return GView;
    }
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论