You must Sign In to post a response.
  • Category: ASP.NET

    How to getrid of probelm occured in gridview

    i hav agridview now i want to programatically add controls to it
    so when i add controls to it and executing textbox control is displaying like
    System.Web.UI.WebControls.TextBox
    and checkbox control is displaying like System.Web.UI.WebControls.TextBox
    how to over come this below is my code so please suggest me where i am wrong

    private void loadDynamicGrid()
    {
    DataTable table = new DataTable();
    TextBox tx = new TextBox();
    CheckBox ab = new CheckBox();
    table.Columns.Add("Dynamic grid", typeof(string));
    table.Columns.Add("Drug", typeof(Control));

    // Here we add five DataRows.
    table.Rows.Add("student first name:", tx);
    table.Rows.Add("student last name:", tx);
    table.Rows.Add("going to school"+ ab,ab);
    table.Rows.Add("school name", tx);
    table.Rows.Add("Grade:");


    foreach (DataColumn col in table.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);
    //TextBox t = new TextBox();


    }

    this.GrdDynamic.DataSource = table;

    //Bind the datatable with the GridView.
    GrdDynamic.DataBind();
    }
    protected void Button1_Click1(object sender, EventArgs e)
    {
    GrdDynamic.Visible = true;
    int grid = Convert.ToInt32(TextBox1.Text);

    for (int i = 0; i < grid; i++)
    {

    loadDynamicGrid();

    }
    Panel1.Controls.Add(GrdDynamic);


    }
  • #762773

    Hai VELIDIRAVIRAM,
    It seems the issue is with the DataField value in your code snippet.
    The DataField value should be the actual value of the Field and not the name of the column.
    You can compare your code with the code which is provided in the below link:

    http://www.aspsnippets.com/Articles/Dynamically-add-BoundField-and-TemplateField-Columns-to-GridView-in-ASPNet.aspx

    Hope it will be helpful to you.


    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com


Sign In to post your comments