Click here to Skip to main content
15,908,254 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to create gridview and boundfields of gridview from code behind in asp.net
Posted
Comments
Rajesh waran 7-Oct-14 8:59am    
Refer this link,
http://asp-net-example.blogspot.in/2008/12/aspnet-boundfield-example-how-to-use.html

1 solution

once go through this code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
            GridView GrdDynamic=new GridView ();
        protected void Page_Load(object sender, EventArgs e)
         {
            loadDynamicGrid();
           panelname.Controls.Add(GrdDynamic);
         }

    private void loadDynamicGrid()
    {
       
        #region Code for preparing the DataTable

        //Create an instance of DataTable
        DataTable dt = new DataTable();

        //Create an ID column for adding to the Datatable
        DataColumn dcol = new DataColumn(ID ,typeof(System.Int32));
        dcol.AutoIncrement = true;
        dt.Columns.Add(dcol);

        //Create an ID column for adding to the Datatable
        dcol = new DataColumn("NAME", typeof(System.String));
        dt.Columns.Add(dcol);

        //Now add data for dynamic columns
        //As the first column is auto-increment, we do not have to add any thing.
        //Let's add some data to the second column.
        for (int nIndex = 0; nIndex < 10; nIndex++)
        {
            //Create a new row
            DataRow drow = dt.NewRow();

            //Initialize the row data.
            drow["NAME"] = "Row-" + Convert.ToString((nIndex + 1));

            //Add the row to the datatable.
            dt.Rows.Add(drow);
        }
        #endregion

        //Iterate through the columns of the datatable to set the data bound field dynamically.
        foreach (DataColumn col in dt.Columns)
        {
            //Declare the bound field and allocate memory for the bound field.
            BoundField bfield = new BoundField();

            //Initalize the DataField value.
            bfield.DataField = col.ColumnName;

            //Initialize the HeaderText field value.
            bfield.HeaderText = col.ColumnName;

            //Add the newly created bound field to the GridView.
            GrdDynamic.Columns.Add(bfield);
        }

        //Initialize the DataSource
        GrdDynamic.DataSource = dt;

        //Bind the datatable with the GridView.
        GrdDynamic.DataBind();
    }
    }
}


write this in aspx page

 <body>
    <form id="form1" runat="server">
 
    <div id="testgrid"  runat="server">
        <asp:panel runat="server" id="pnael34" xmlns:asp="#unknown">
        </asp:panel>
    </div>
    </form>
</body>
</body>
 
Share this answer
 
v2

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