C# 在Visual Studio C中向表中添加行#
我试图在每次单击按钮时向对象表添加新行,但当我单击按钮时,它只会编辑和覆盖同一行C# 在Visual Studio C中向表中添加行#,c#,asp.net,visual-studio-2012,C#,Asp.net,Visual Studio 2012,我试图在每次单击按钮时向对象表添加新行,但当我单击按钮时,它只会编辑和覆盖同一行 protected void btAgregaT_Click(object sender, EventArgs e) { TableRow t = new TableRow(); TableCell c = new TableCell(); c.Text = DDLArticulos.SelectedItem.ToString(); t.Cells.Add(c); Tab
protected void btAgregaT_Click(object sender, EventArgs e)
{
TableRow t = new TableRow();
TableCell c = new TableCell();
c.Text = DDLArticulos.SelectedItem.ToString();
t.Cells.Add(c);
TableCell c2 = new TableCell();
c2.Text = TBCantidad.Text;
t.Cells.Add(c2);
tablaPed.Rows.Add(t);
}
您可以向DataTable添加新行,如下所示:
var newRow = tablaPed.NewRow();
newRow["Column1"] = "Value1";
newRow["Column3"] = "Value2";
newRow["Column3"] = "Value3";
tablaPed.Rows.Add(newRow);
您可以向DataTable添加新行,如下所示:
var newRow = tablaPed.NewRow();
newRow["Column1"] = "Value1";
newRow["Column3"] = "Value2";
newRow["Column3"] = "Value3";
tablaPed.Rows.Add(newRow);
更新2:
基本上,您需要使用ViewState来管理一切。下面我还提到了添加,简单地说,您可以从ViewState添加、编辑、删除记录,并将它们绑定到gridview
protected void Page_Load(object sender, EventArgs e)
{
try
{
//Adding data to datatable
DataRow t = tablaPed.NewRow();
//adding data to row
t["SrNo"] = "1";
t["EmailId"] = "test@gmail.com";
t["Password"] = "Password";
//adding row to table
tablaPed.Rows.Add(t);
//saving databale into viewstate
ViewState["UserDetail"] = t;
//bind Gridview
GridView1.DataSource = t;
GridView1.DataBind();
}
}
下面是单击“添加”按钮时要调用的函数:
private void AddNewRecordRowToGrid()
{
// check view state is not null
if (ViewState["UserDetail"] != null)
{
//get datatable from view state
DataTable dtCurrentTable = (DataTable)ViewState["UserDetail"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
//this will add previously added entries
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
//add each row into data table
drCurrentRow = dtCurrentTable.NewRow();
//you can add data from textbox or other user control
drCurrentRow["SrNo"] = "2";
drCurrentRow["EmailId"] = "test@gmail.com";
drCurrentRow["Password"] = "Password";
}
//Remove initial blank row
if (dtCurrentTable.Rows[0][0].ToString() == "")
{
dtCurrentTable.Rows[0].Delete();
dtCurrentTable.AcceptChanges();
}
//add created Rows into dataTable
dtCurrentTable.Rows.Add(drCurrentRow);
//Save Data table into view state after creating each row
ViewState["UserDetail"] = dtCurrentTable;
//Bind Gridview with latest Row
GridView1.DataSource = dtCurrentTable;
GridView1.DataBind();
}
}
}
protected void btAgregaT_Click(object sender, EventArgs e)
{
AddNewRecordRowToGrid();
}
就你而言:
protected void btAgregaT_Click(object sender, EventArgs e)
{
//cloning the table row
DataRow t = tablaPed.NewRow();
//adding data to row
t["SrNo"] = "1";
t["EmailId"] = "test@gmail.com";
t["Password"] = "Password";
//adding row to table
tablaPed.Rows.Add(t);
}
更新2:
基本上,您需要使用ViewState来管理一切。下面我还提到了添加,简单地说,您可以从ViewState添加、编辑、删除记录,并将它们绑定到gridview
protected void Page_Load(object sender, EventArgs e)
{
try
{
//Adding data to datatable
DataRow t = tablaPed.NewRow();
//adding data to row
t["SrNo"] = "1";
t["EmailId"] = "test@gmail.com";
t["Password"] = "Password";
//adding row to table
tablaPed.Rows.Add(t);
//saving databale into viewstate
ViewState["UserDetail"] = t;
//bind Gridview
GridView1.DataSource = t;
GridView1.DataBind();
}
}
下面是单击“添加”按钮时要调用的函数:
private void AddNewRecordRowToGrid()
{
// check view state is not null
if (ViewState["UserDetail"] != null)
{
//get datatable from view state
DataTable dtCurrentTable = (DataTable)ViewState["UserDetail"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
//this will add previously added entries
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
//add each row into data table
drCurrentRow = dtCurrentTable.NewRow();
//you can add data from textbox or other user control
drCurrentRow["SrNo"] = "2";
drCurrentRow["EmailId"] = "test@gmail.com";
drCurrentRow["Password"] = "Password";
}
//Remove initial blank row
if (dtCurrentTable.Rows[0][0].ToString() == "")
{
dtCurrentTable.Rows[0].Delete();
dtCurrentTable.AcceptChanges();
}
//add created Rows into dataTable
dtCurrentTable.Rows.Add(drCurrentRow);
//Save Data table into view state after creating each row
ViewState["UserDetail"] = dtCurrentTable;
//Bind Gridview with latest Row
GridView1.DataSource = dtCurrentTable;
GridView1.DataBind();
}
}
}
protected void btAgregaT_Click(object sender, EventArgs e)
{
AddNewRecordRowToGrid();
}
就你而言:
protected void btAgregaT_Click(object sender, EventArgs e)
{
//cloning the table row
DataRow t = tablaPed.NewRow();
//adding data to row
t["SrNo"] = "1";
t["EmailId"] = "test@gmail.com";
t["Password"] = "Password";
//adding row to table
tablaPed.Rows.Add(t);
}
DataRow
的对象不能是新的,它应该是指您已经全局拥有的数据表
protected void btAgregaT_Click(object sender, EventArgs e)
{
TableRow t = tablaPed.TableRow();
TableCell c = new TableCell();
c.Text = DDLArticulos.SelectedItem.ToString();
t.Cells.Add(c);
TableCell c2 = new TableCell();
c2.Text = TBCantidad.Text;
t.Cells.Add(c2);
tablaPed.Rows.Add(t);
}
此外,如果在添加新项之前为dataTable调用了
refresh()
或clear()
方法,请检查代码。如果确实找到了某个对象,请对其进行注释,然后尝试执行。数据行的对象不能是新的,它应该引用全局已有的数据表
protected void btAgregaT_Click(object sender, EventArgs e)
{
TableRow t = tablaPed.TableRow();
TableCell c = new TableCell();
c.Text = DDLArticulos.SelectedItem.ToString();
t.Cells.Add(c);
TableCell c2 = new TableCell();
c2.Text = TBCantidad.Text;
t.Cells.Add(c2);
tablaPed.Rows.Add(t);
}
此外,如果在添加新项之前为dataTable调用了
refresh()
或clear()
方法,请检查代码。如果确实找到了某个内容,请对其进行注释,然后尝试执行。tablaPed的类型是DataTable
tablaPed.Rows.Add(DDLArticulos.SelectedItem.ToString(),TBCantidad.Text)
tablaPed的类型是DataTable
tablaPed.Rows.Add(DDLArticulos.SelectedItem.ToString(),TBCantidad.Text)代码>您好,谢谢您的帮助,但问题是当我单击按钮(它位于网页中)时,它只添加一行,并且每次单击按钮时都会覆盖它。完美,ViewState和会话之间有什么区别?我也可以使用它吗?当然,存储在会话变量中的数据将保留整个用户会话,而viewstate将仅保留该页面:)。如果我有帮助的话,别忘了投票。嗨,谢谢你的帮助,但问题是当我点击按钮(它在网页中)时,它只添加了一行,并且每次点击按钮都会覆盖它。完美,ViewState和会话之间有什么区别?我也可以使用它吗?当然,存储在会话变量中的数据将保留整个用户会话,而viewstate将仅保留该页面:)。如果我有什么帮助,别忘了投票。