Click here to Skip to main content
15,914,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a gridview 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

C#
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);


}
Posted
Updated 25-Oct-15 19:55pm
v2
Comments
[no name] 8-Oct-15 10:48am    
If gridview columns are not dynamic then use TemplateField.

http://www.aspsnippets.com/Articles/Adding-Dynamic-Rows-in-ASP.Net-GridView-Control-with-TextBoxes.aspx

Secondly you are adding Textbox object to DataTale so you are getting System.Web.UI.

1 solution

When we add dynamic textbox,checkbox or any control to gridview we have to add in a specific row with column, not in datatable[^].

C#
TextBox tx = new TextBox(); // this line of code create an object of TextBox, where is the ID, runat, etc. attributes.

then when you try to add TextBox in DataTable(which is wrong)
C#
table.Rows.Add("student first name:", tx); // tx object will take as string and save System.Web.UI.WebControls.TextBox in datatable column.


For Adding controls to GridView first of all add templatefield to gridview than on rowdatabound create controls of specific class with ID to add in that templatefield.

Here is a link[^] that will help you
 
Share this answer
 
Comments
raviram123 12-Oct-15 5:12am    
thank u
very nice what u told and is correct
Rojalin Sahoo 13-Oct-15 0:36am    
You welcome. It's my pleasure that it solves your problem.

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