Creating a GridView with columns generated at runtime

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 this, and if it’s not possible, how I can output the DataTable into nicely formatted HTML.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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);
}

Method 2

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.

Method 3

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;
    }


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x