向现有数据表添加新行的 C# 问题
本文关键字:问题 新行 添加 数据表 | 更新日期: 2023-09-27 18:35:57
我正在尝试在单击时添加新行。但是,它取代了我添加到新行的现有行即时。
以下是代码:
主.cs
protected void Button1_Click(object sender, EventArgs e)
{
DateTime selectedDate = CalendarMain.SelectedDate;
string SportType = ddlSportType.SelectedItem.ToString();
string distance = ddlDistance.SelectedItem.ToString();
plan wp = new plan();
wp.Time_Start = selectedDate;
wp.Duration = distance;
wp.Activity = SportType;
DataTable dt = wp.addPlanDetailDataRow();
gvActivityList.DataSource = dt;
gvActivityList.DataBind();
}
计划.cs
public DataTable addPlanDetailDataRow()
{
DataTable dt = new DataTable();
dt.Columns.Add("Activity");
dt.Columns.Add("Duration");
dt.Columns.Add("status");
dt.Columns.Add("Time_Start");
dt.Columns.Add("Plan_ID");
DataRow newRow = dt.NewRow();
dt.Rows.Add(newRow);
return dt;
}
我不确定我错过了什么。请指导我关于我的错误。多谢
您的数据表是在 addPlanDetailDataRow
中本地定义的。它应该在类范围内定义,并嵌入一次以保留以前的值。以某种方法在数据表中添加列为例,以便调用一次。
DataTable dt = new DataTable();
private InitDataTable() //This method should be called once
{
dt.Columns.Add("Activity");
dt.Columns.Add("Duration");
dt.Columns.Add("status");
dt.Columns.Add("Time_Start");
dt.Columns.Add("Plan_ID");
}
public DataTable addPlanDetailDataRow()
{
DataRow newRow = dt.NewRow();
dt.Rows.Add(newRow);
return dt;
}
编辑
由于数据表声明为数据成员,并且类的所有方法都可以访问,因此不需要返回数据表。这将更改 addPlanDetailDataRow,如下所示。另请注意,您只是添加行而不是填充行。
public void addPlanDetailDataRow()
DataRow newRow = dt.NewRow();
newRow["Activity"] = "a1"; //These dummy values should be replaced by real values.
newRow["Duration"] = "d1";
newRow["status"] = "s1";
newRow["Time_Start"] = "st1";
newRow["Plan_ID"] = "p1";
dt.Rows.Add(newRow);
}
方法的调用将是
gvActivityList.DataSource = addPlanDetailDataRow();
gvActivityList.DataBind();
由于 DataBind 在 asp.net 中使用,因此可能需要将数据表放在 ViewState 中或将数据放在数据库中。
使用 ViewState 记住以前的值:
编辑 :-
protected void Button1_Click(object sender, EventArgs e)
{
DateTime selectedDate = CalendarMain.SelectedDate;
string SportType = ddlSportType.SelectedItem.ToString();
string distance = ddlDistance.SelectedItem.ToString();
plan wp = new plan();
wp.Time_Start = selectedDate;
wp.Duration = distance;
wp.Activity = SportType;
DataTable dt = new DataTable();
dt.Columns.Add("Activity");
dt.Columns.Add("Duration");
dt.Columns.Add("status");
dt.Columns.Add("Time_Start");
dt.Columns.Add("Plan_ID");
DataTable dt1 = wp.addPlanDetailDataRow(dt);
gvActivityList.DataSource = dt;
gvActivityList.DataBind();
}
public DataTable addPlanDetailDataRow(DataTable dt)
{
if (ViewState["Datatable"] != null)
{
dt = (DataTable)ViewState["Datatable"];
}
ViewState["Datatable"] = dt;
DataRow dr = dt.NewRow();
dr["Activity"]="value1";
dr["Duration"]="value2";
dr["status"]="value3";
dr["Time_Start"]="value4";
dr["Plan_ID"]="value5";
dt.Rows.Add(dr);
return dt;
}
我已经尝试了一个示例页面,它的工作原理请参见下面的代码。
回发后持久化数据 尝试将数据表放在视图状态中并查看
视图状态,通过启用跟踪。如果它不是太大且小于 1 MB,则使用视图状态,
因为它更高效,不使用任何服务器资源。
主.cs
protected void btn1_Click(object sender, EventArgs e)
{
DateTime selectedDate = DateTime.Now;
string SportType = "Dummy SportType";
string distance = "Dymmy distance";
plan wp = new plan();
wp.Time_Start = selectedDate;
wp.Duration = distance;
wp.Activity = SportType;
DataTable dt;
if (ViewState["Datatable"] != null) //
{
wp.dt = (DataTable)ViewState["Datatable"];
}
else
{
wp.InitDataTable();
}
dt = wp.addPlanDetailDataRow();
ViewState["Datatable"] = dt;
gvActivityList.DataSource = dt;
gvActivityList.DataBind();
}
计划.cs
public plan()
{
//
// TODO: Add constructor logic here
//
}
public DataTable dt;
public void InitDataTable()
{
dt = new DataTable();
dt.Columns.Add("Activity");
dt.Columns.Add("Duration");
dt.Columns.Add("status");
dt.Columns.Add("Time_Start");
dt.Columns.Add("Plan_ID");
}
public DateTime Time_Start;
public string Duration;
public string Activity;
public DataTable addPlanDetailDataRow()
{
DataRow newRow = dt.NewRow();
newRow["Activity"] = this.Activity;
newRow["Duration"] = this.Duration;
newRow["status"] = "s1";
newRow["Time_Start"] = this.Time_Start;
newRow["Plan_ID"] = "p1";
dt.Rows.Add(newRow);
return dt;
}