Click here to Skip to main content
14,668,434 members
Rate this:
Please Sign up or sign in to vote.
See more:
pls help me i want to create gridview columns dynamically based on the column data in a table
Posted

Rate this:
Please Sign up or sign in to vote.

Solution 1

Hi Pyla,

Please do the following:

1. set AutoColumn=True in your datagrid
2. create columns based on your logic like
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
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

public static DataTable dtValues;

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

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


hope that works..
Amit
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 3

check this codeproject article how to create columns dynamically in a grid view[]
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100