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

    How to insert label,textbox,checkbox button controls in gridview

    ihave written code for when i insert some value in textbox gridview should be displayed dynamically how many i have entered in textbox

    so my task is to add controls to it
    manually or programatically

    manually not taking its not taking itemtemplate

    i want my gridview like
    labelcontrols
    studentfirstname textbox this iscontrol
    studentlastname textboxthis iscontrol
    going to school checkbox1,checkbox2this iscontrol
    scchoolname textboxthis iscontrol
    grade dropdownlistcontrol
    buttoncontrol
    but i am not getting where iam wrong i am sending my .aspx and .aspx.cs code
    plz suggest me where i am wrong

    /.aspx
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="dynamic.aspx.cs" Inherits="dynamic" %>

    <!DOCTYPE html>


    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
    <title></title>

    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <table border="0" cellpadding="0" cellspacing="0">

    <tr>
    <td>
    <asp:GridView ID="GrdDynamic" runat="server" AutoGenerateColumns="False" CssClass="auto-style1" Height="142px" Width="105px">

    </asp:GridView>
     <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click1" Text="Button" />

    </td>
    </tr>
    </table>
    <asp:Panel ID="Panel1" runat="server">
    </asp:Panel>
    </div>
    <asp:Panel ID="Panel2" runat="server">
    </asp:Panel>
    </form>
    </body>
    </html>


    .aspx.cs


    using System;
    using System.IO;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.SqlClient;
    public partial class dynamic : System.Web.UI.Page
    {
    //#region constants
    const string Dynamicgrid = "namea";



    //#endregion

    protected void Page_Load(object sender, EventArgs e)
    {
    }
    private void loadDynamicGrid()
    {
    //#region Code for preparing the DataTable

    DataTable dt = new DataTable();


    DataColumn dcol = new DataColumn(Dynamicgrid, typeof(System.String));


    dt.Columns.Add(dcol);



    for (int nIndex = 0; nIndex < 4; nIndex++)
    {

    //Create a new row
    DataRow drow = dt.NewRow();
    //DataRow dr = dt.NewRow();
    //DataRow dra = dt.NewRow();
    DataRow dr = dt.NewRow();
    //Initialize the row data.
    //drow[Dynamicgrid] = "Row-" + Convert.ToString((nIndex + 1));
    drow[Dynamicgrid] = "1";
    drow[Dynamicgrid] = "studentlastname:";
    drow[Dynamicgrid] = "studentfirstname:";
    drow[Dynamicgrid] = "going to school";
    drow[Dynamicgrid] = "school name";
    drow[Dynamicgrid] = "Grade";

    //dr[sra] = "Going to school";
    ////Add the row to the datatable.
    dt.Rows.Add(drow);
    dt.AcceptChanges();
    }
    //#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);


    }this.GrdDynamic.DataSource = dt;

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



    protected void Button1_Click1(object sender, EventArgs e)
    {


    int grid = Convert.ToInt32(TextBox1.Text);
    for (int i = 0; i < grid; i++)
    {

    loadDynamicGrid();
    }
    Panel1.Controls.Add(GrdDynamic);

    }
    }
  • #766763
    Hi,

    As per my understanding you want to generate dynamic gridviews based on entry in textbox control, how many that is second point but before that we should know how to create dynamic gridviews with boundfield, templatefield etc.. If you know this then automatically you know how to bind no of gridviews based on textbox entry, for understanding how to generate dynamic gridview you should refer below link

    http://www.dotnetfox.com/articles/create-dynamic-gridview-or-programmatically-create-Asp-Net-gridview-with-dynamic-boundfield-1083.aspx

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #766766
    [Response removed by Admin. Read forum policies.]

  • #766877
    you want dynamically create button ,checboxes , textbox controls for a datagrid and another thing is you want add rows and columns dynamically to a table please refer the following url for these. with source code available.
    http://www.c-sharpcorner.com/uploadfile/raj1979/datagridview-control-in-C-Sharp/

    SRI RAMA PHANI BHUSHAN KAMBHAMPATI

  • #766940
    Hi,
    Try this code:
    .ASPX Page:
    Add OnRowDataBound() event for gridview as follows:

    <asp:GridView ID="GrdDynamic" runat="server" AutoGenerateColumns="False" CssClass="auto-style1" Height="142px" Width="90%" OnRowDataBound="GrdDynamic_RowDataBound"> </asp:GridView>


    .CS Page:

    protected void Page_Load(object sender, EventArgs e)
    {
    }

    private void loadDynamicGrid(int grid)
    {
    BoundField boundField = new BoundField();
    boundField.HeaderText = "Serial No.";
    boundField.DataField = "SrNo";
    GrdDynamic.Columns.Add(boundField);

    TemplateField tmpField = new TemplateField();
    tmpField.HeaderText = "Student First Name";
    GrdDynamic.Columns.Add(tmpField);

    tmpField = new TemplateField();
    tmpField.HeaderText = "Student Last Name";
    GrdDynamic.Columns.Add(tmpField);

    tmpField = new TemplateField();
    tmpField.HeaderText = "Student Going to School?";
    GrdDynamic.Columns.Add(tmpField);

    tmpField = new TemplateField();
    tmpField.HeaderText = "Student School Name";
    GrdDynamic.Columns.Add(tmpField);

    tmpField = new TemplateField();
    tmpField.HeaderText = "Student Grade";
    GrdDynamic.Columns.Add(tmpField);

    DataTable dtTable = new DataTable();
    dtTable.Columns.AddRange(new DataColumn[6] { new DataColumn("SrNo", typeof(int)),
    new DataColumn("FirstName", typeof(string)),
    new DataColumn("LastName",typeof(string)),
    new DataColumn("GoingSchool", typeof(bool)),
    new DataColumn("SchoolName", typeof(string)),
    new DataColumn("Grade", typeof(string))});

    int iSrNo = 0;
    for (int nIndex = 0; nIndex < grid; nIndex++)
    {
    iSrNo = nIndex + 1;
    dtTable.Rows.Add(iSrNo, "Fname " + iSrNo, "Lname " + iSrNo, true, "School " + iSrNo, "Grade " + iSrNo);
    }

    GrdDynamic.DataSource = dtTable;
    GrdDynamic.DataBind();
    }

    protected void Button1_Click1(object sender, EventArgs e)
    {
    int grid = Convert.ToInt32(TextBox1.Text);
    //for (int i = 0; i < grid; i++)
    //{
    loadDynamicGrid(grid);
    //}
    Panel1.Controls.Add(GrdDynamic);
    }

    protected void GrdDynamic_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    TextBox txtName;
    CheckBox chkSchool;
    DropDownList dlstGrade;

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    txtName = new TextBox();
    txtName.ID = "txtFname" + e.Row.RowIndex;
    txtName.Text = (e.Row.DataItem as DataRowView).Row["FirstName"].ToString();
    e.Row.Cells[1].Controls.Add(txtName);

    txtName = new TextBox();
    txtName.ID = "txtLname" + e.Row.RowIndex;
    txtName.Text = (e.Row.DataItem as DataRowView).Row["LastName"].ToString();
    e.Row.Cells[2].Controls.Add(txtName);

    chkSchool = new CheckBox();
    chkSchool.ID = "chkSchool" + e.Row.RowIndex;
    chkSchool.Checked = Convert.ToBoolean((e.Row.DataItem as DataRowView).Row["GoingSchool"]);
    e.Row.Cells[3].Controls.Add(chkSchool);

    txtName = new TextBox();
    txtName.ID = "txtSchoolname" + e.Row.RowIndex;
    txtName.Text = (e.Row.DataItem as DataRowView).Row["SchoolName"].ToString();
    e.Row.Cells[4].Controls.Add(txtName);

    dlstGrade = new DropDownList();
    dlstGrade.ID = "dlstGrade" + e.Row.RowIndex;
    dlstGrade.Items.Add("");
    dlstGrade.Items.Add(new ListItem((e.Row.DataItem as DataRowView).Row["Grade"].ToString()));
    dlstGrade.SelectedIndex = 1;
    e.Row.Cells[5].Controls.Add(dlstGrade);
    }
    }


Sign In to post your comments