datatable 插入行怎么只能插一次?如何做到点一次按钮插入一条,之前内容保留

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="inproduct.aspx.cs" Inherits="inproduct" %>

<!DOCTYPE html>













/asp:GridView
    </div>
</form>


后台程序

using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class inproduct : System.Web.UI.Page
{

public DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{

    DataColumn dc1 = new DataColumn("prizename", Type.GetType("System.String"));
    DataColumn dc2 = new DataColumn("point", Type.GetType("System.Int16"));
    DataColumn dc3 = new DataColumn("number", Type.GetType("System.Int16"));
    DataColumn dc4 = new DataColumn("totalpoint", Type.GetType("System.Int64"));
    DataColumn dc5 = new DataColumn("prizeid", Type.GetType("System.String"));
    dt.Columns.Add(dc1);
    dt.Columns.Add(dc2);
    dt.Columns.Add(dc3);
    dt.Columns.Add(dc4);
    dt.Columns.Add(dc5);
    DataRow dr = dt.NewRow();
    dr["prizename"] = "娃娃1";
    dr["point"] = 10;
    dr["number"] = 1;
    dr["totalpoint"] = 10;
    dr["prizeid"] = "001";
    dt.Rows.Add(dr);
    GridView1.DataSource = dt;
    GridView1.DataBind();
}


protected void Button1_Click(object sender, EventArgs e)
{
    DataRow dr = dt.NewRow();
    dr["prizename"] = "娃娃2";
    dr["point"] = 20;
    dr["number"] = 2;
    dr["totalpoint"] = 20;
    dr["prizeid"] = "002";
    dt.Rows.Add(dr);
    GridView1.DataSource = dt;
    GridView1.DataBind();
}

protected void Button2_Click(object sender, EventArgs e)
{
    DataRow dr2 = dt.NewRow();
    dr2["prizename"] = "娃娃3";
    dr2["point"] = 30;
    dr2["number"] = 3;
    dr2["totalpoint"] = 30;
    dr2["prizeid"] = "003";
    dt.Rows.Add(dr2);

    GridView1.DataSource = dt;
    GridView1.DataBind();
}

}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问