Click here to Skip to main content
15,882,114 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
pls help me i want to create gridview columns dynamically based on the column data in a table
Posted

Hi Pyla,

Please do the following:

1. set AutoColumn=True in your datagrid
2. create columns based on your logic like
C#
DataTable dt = new DataTable();
        for (int c = 1; c <= 10; c++)
        {
            DataColumn dc = new DataColumn("Column" + c.ToString(), System.Type.GetType("System.String"));
            dt.Columns.Add(dc);
        }

        GridView1.Datasource = dt;
        GridView1.DataBind();


Please do let me know, if you have any doubt.

Please provide Vote if this would be helpful to you.

Thanks,
Imdadhusen
 
Share this answer
 
public static DataTable dtValues;

C#
protected void CreateDataTable()
       {
           dtValues = new DataTable();
           dtValues.Columns.Add("Id");
           dtValues.Columns.Add("Item");
           dtValues.Columns.Add("Rate");
           dtValues.Columns.Add("Qty");
           dtValues.Columns.Add("Amount");
       }



these 5 columns will be created dynamically

than add it

MIDL
gvOrders.DataSource = dtValues;
                gvOrders.DataBind();


hope that works..
Amit
 
Share this answer
 
v2
check this codeproject article how to create columns dynamically in a grid view[]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900