我正在制作 ASP.net 应用程序,我想在那里有某种表格。我尝试使用 Grindview,但是当我尝试添加第二个新行(来自代码)时,第二行替换了第一行。
代码如下:
DataTable dt = new DataTable();
if (dt.Columns.Count == 0)
{
dt.Columns.Add("thing", typeof(string));
dt.Columns.Add("thing2", typeof(string));
}
DataRow NewRow = dt.NewRow();
NewRow[0] = label1.Text;
NewRow[1] = label2.Text;
dt.Rows.Add(NewRow);
GridView1.DataSource = dt;
GridView1.DataBind();
我期待 Gridview 有 2 行和按钮,每次点击都会给你新的行。
最佳答案
我不知道你到底在问什么或想做什么,但这可能会有所帮助
protected void Button1_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
if (dt.Columns.Count == 0)
{
dt.Columns.Add("nameofcolumn1", typeof(string));
dt.Columns.Add("nameofcolumn2", typeof(string));
dt.Columns.Add("nameofcolumn3", typeof(string));
}
DataRow NewRow = dt.NewRow();
NewRow[0] = value1;
NewRow[ 1] = value2;
dt.Rows.Add(NewRow);
GridView1.DataSource = dt;
GridViewl.DataBind();
}
关于c# - 如何向 GridView 添加新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38059884/