当我尝试向前一个粉碎的数据集中添加记录时,我有一个表单,每次点击button1后我都会将数据添加到数据集中,并且每次只找到我的上一个记录 这是我的代码
public partial class _Default : System.Web.UI.Page
{
private DataTable dt=new DataTable("manager");
private DataSet data_set=new DataSet("Test");
private static int _stagiaireid = 0;
public static int StagiaireID { get { return _stagiaireid++; } }
protected void Page_Load(object sender, EventArgs e)
{
//dt = new DataTable("manager");
dt.Columns.Add("id ", typeof(int));
dt.Columns.Add("electro ", typeof(string));
dt.Columns.Add("sn", typeof(string));
dt.Columns.Add("date ", typeof(string));
dt.Columns.Add("email", typeof(string));
dt.Columns.Add("marque ", typeof(string));
DropDownList1.Items.Add(new ListItem("Siera", "Siera"));
DropDownList1.Items.Add(new ListItem("AB","AB"));
DropDownList1.Items.Add(new ListItem("CD","CD"));
data_set.Tables.Add(dt);
GridView1.DataSource = data_set.Tables["manager"];
}
protected void Button1_Click(object sender, EventArgs e)
{
String electro = TextBox1.Text;
String sn = TextBox2.Text;
String date = TextBox3.Text;
String email = TextBox4.Text;
String marque = DropDownList1.SelectedValue.ToString() ;
if (aide.nom_pre(email)==true)
{
Label1.Text = "Email Juste";
}
else
{
Label1.Text = "Email Faux";
return;
}
data_set.Tables["manager"].Rows.Add(StagiaireID, electro, sn, date, email, marque);
// dt.Rows.Add(StagiaireID,electro, sn, date, email, marque);
GridView1.DataBind();
}
答案 0 :(得分:1)
当然,您只会获得最后一条记录,因为您在每个回发中创建并填充DataSet
和DataTable
并且您没有将DataSet
存储在会话状态中你也没绑定GridView
将您的代码更改为:
private DataTable dt = new DataTable("manager");
private DataSet data_set = new DataSet("Test");
private static int _stagiaireid = 0;
public static int StagiaireID { get { return _stagiaireid++; } }
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dt.Columns.Add("id ", typeof(int));
dt.Columns.Add("electro ", typeof(string));
dt.Columns.Add("sn", typeof(string));
dt.Columns.Add("date ", typeof(string));
dt.Columns.Add("email", typeof(string));
dt.Columns.Add("marque ", typeof(string));
data_set.Tables.Add(dt);
GridView1.DataSource = data_set.Tables["manager"];
this.ViewState["ds"] = data_set;
GridView1.DataBind();
}
else
{
data_set = (DataSet)ViewState["ds"];
}
}
protected void Button1_Click(object sender, EventArgs e)
{
String electro = TextBox1.Text;
String sn = TextBox2.Text;
String date = TextBox3.Text;
String email = TextBox4.Text;
String marque = DropDownList1.SelectedValue.ToString();
if (aide.nom_pre(email) == true)
{
Label1.Text = "Email Juste";
}
else
{
Label1.Text = "Email Faux";
return;
}
data_set.Tables["manager"].Rows.Add(StagiaireID, electro, sn, date, email, marque);
GridView1.DataSource = data_set.Tables["manager"];
GridView1.DataBind();
this.ViewState["ds"] = data_set;
}
答案 1 :(得分:0)
问题是你没有在帖子后面保留数据集。所以保持你的数据集在会话中。请参阅ASP.NET中的状态管理机制
答案 2 :(得分:0)
在将数据表绑定到网格视图之前,请在已添加行的数据表上调用AcceptChanges()
方法,然后再次使用此表将DataSource
分配给网格视图。
data_set.Tables["manager"].Rows.Add(StagiaireID, electro, sn, date, email, marque);
data_set.Tables["manager"].AcceptChanges();
GridView1.DataSource = data_set.Tables["manager"];
GridView1.DataBind();