如何根据用户在网络上的输入生成数据网格行,因此如果我在文本框中输入5,则将生成5行,反之亦然。
我在下面尝试过这个代码但每次尝试运行程序时都会出现Exception of type 'System.OutOfMemoryException' was thrown
错误
我的aspx.cs
private DataTable TempTable
{
get { return (DataTable)ViewState["TempTable"]; }
set { ViewState["TempTable"] = value; }
}
public void dtTemp()
{
TempTable = new DataTable();
TempTable.Columns.Add("ID_", typeof(string));
TempTable.Columns.Add("Name_", typeof(string));
TempTable.Columns.Add("Phone_", typeof(string));
}
protected void btnAdd_Click(object sender, ImageClickEventArgs e)
{
int rowTotal = Int32.Parse(totalRow.Text);
int i = 0;
panelTest.Visible = true;
dtTemp();
DataRow dr = TempTable.NewRow();
for (; ; )
{
if (i < rowTotal)
{
dr = TempTable.NewRow();
dr["ID_"] = "";
dr["Name_"] = "";
dr["Phone_"] = "";
TempTable.Rows.Add(dr);
grid1.DataSource = TempTable;
grid1.DataBind();
}
else
{
break;
}
}
}
答案 0 :(得分:0)
你必须在下面尝试这个,在(;;)
的永久循环中增加i ++ protected void btnAdd_Click(object sender, ImageClickEventArgs e)
{
int rowTotal = Int32.Parse(totalRow.Text);
int i = 0;
panelTest.Visible = true;
dtTemp();
DataRow dr = TempTable.NewRow();
for (; ; )
{
if (i < rowTotal)
{
dr = TempTable.NewRow();
dr["ID_"] = "";
dr["Name_"] = "";
dr["Phone_"] = "";
TempTable.Rows.Add(dr);
grid1.DataSource = TempTable;
grid1.DataBind();
}
else
{
break;
}
i++;
}
}
答案 1 :(得分:0)
我之前的代码崩溃了我的视觉工作室,它说我没有足够的内存所以我改变了我的代码并且它有效。
public DataTable TempTable
{
get; set;
}
public void dtTemp()
{
TempTable = new DataTable();
TempTable.Columns.Add("ID_", typeof(string));
TempTable.Columns.Add("Name_", typeof(string));
TempTable.Columns.Add("Phone_", typeof(string));
}
protected void btnAdd_Click(object sender, ImageClickEventArgs e)
{
int rowTotal = Int32.Parse(txtRow.Text);
dtTemp();
DataRow dr = TempTable.NewRow();
for (int i = 0; i < rowTotal; i++)
{
dr = TempTable.NewRow();
dr["ID_"] = "";
dr["Name_"] = "";
dr["Phone_"] = "";
TempTable.Rows.Add(dr);
}
grid1.DataSource = TempTable;
grid1.DataBind();
}