问题描述
我有一个gridview现在我想以编程方式添加控件
所以当我添加控件并执行文本框控件时显示如
System.Web。 UI.WebControls.TextBox
和复选框控件显示如System.Web.UI.WebControls.TextBox
如何结束以下是我的代码所以请建议我在哪里我错了
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
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);
}
推荐答案
TextBox tx = new TextBox(); // this line of code create an object of TextBox, where is the ID, runat, etc. attributes.
然后当你尝试在DataTable中添加TextBox时(这是错误的)
then when you try to add TextBox in DataTable(which is wrong)
table.Rows.Add("student first name:", tx); // tx object will take as string and save System.Web.UI.WebControls.TextBox in datatable column.
为了向GridView添加控件首先将templatefield添加到gridview而不是在rowdatabound上创建具有ID的特定类的控件以添加templatefield。
这是 []将帮助你
这篇关于如何在gridview中发生问题的getrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!