C#向现有数据表添加新行时出现问题

时间:2015-01-02 05:02:39

标签: c# asp.net .net

我正在尝试在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;
}

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

3 个答案:

答案 0 :(得分:5)

您的DataTable在addPlanDetailDataRow本地定义。它应该在类范围中定义,并且 instanstiated 一次以保留先前的值。以某种方法在datatable中添加for列,以便调用一次。

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

修改

由于DataTable被声明为数据成员并且可以被类的所有方法访问,因此您不需要返回数据表。这将更改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中使用的那样,您可能需要将DataTable放在ViewState中或将数据放在DataBase中。

答案 1 :(得分:3)

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

答案 2 :(得分:1)

我已经尝试了一个示例页面,其工作原理见下面的代码。

//在PostBack尝试将数据表放入Viewstate并查看

的大小后保留数据

// Viewstate,启用跟踪。如果它不是太大而且小于1 MB,那么使用Viewstate,

//因为它效率更高,不使用任何服务器资源。

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

}

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