如何在gridview中发生问题的getrid [英] How to getrid of probelm occured in gridview

查看:58
本文介绍了如何在gridview中发生问题的getrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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);


}

推荐答案

当我们向gridview添加动态文本框,复选框或任何控件时必须在列中添加特定行,而不是在数据表中 [ ^ ]。



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

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆