gpt4 book ai didi

c# - C#问题,向现有数据表添加新行

转载 作者:太空宇宙 更新时间:2023-11-03 17:50:45 24 4
gpt4 key购买 nike

我试图在onClick上添加新行。但是,它取代了我现有的添加到新行的行瞬间。

以下是代码:

Main.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();
}


plan.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;
}


我不确定我错过了什么。请指导我有关我的错误。非常感谢

最佳答案

使用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;
}

关于c# - C#问题,向现有数据表添加新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27737568/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com