Vishal Gavle
Vishal Gavle

Reputation: 367

How to Add Dynamic Column to GridView

I want to add dynamic columns and datatype to gridview in ASP.Net, so I can assign datasource as a data table

Upvotes: 0

Views: 173

Answers (1)

nunespascal
nunespascal

Reputation: 17724

Add the columns to the DataTable, populate your data and then assign the DataTable to the DataSource of the gridview.

DataTable workTable = new DataTable("Customers");

DataColumn workCol = workTable.Columns.Add("CustID", typeof(Int32));
workCol.AllowDBNull = false;
workCol.Unique = true;

workTable.Columns.Add("CustLName", typeof(String));
workTable.Columns.Add("CustFName", typeof(String));
workTable.Columns.Add("Purchases", typeof(Double));

Refer: Adding Columns to a DataTable

Upvotes: 1

Related Questions