简体   繁体   中英

C# Issue with adding new row to existing datatable

I am trying to add a new row upon onClick. However, it replaced my existing row instant of adding to new row.

Here are the codes:

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

I am not sure what i have missed out. Please guide me about my error. Thanks a lot

Your DataTable is defined locally in addPlanDetailDataRow . It should be defined in class scope and instanstiated once to keep the previous values. Take the for adding columns in datatable in some method so that it is called once.

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

Edit

As the DataTable is declared as data member and is accessible to all methods of the class you do not need to return the datatable. This will change the addPlanDetailDataRow as show below. Also note that you are just adding the rows but not filling the rows.

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

Calling of method would be

gvActivityList.DataSource = addPlanDetailDataRow();
gvActivityList.DataBind();

As DataBind is using in asp.net you might need to put the DataTable in ViewState or data in DataBase.

Use ViewState to remember previous values :

EDIT :-

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

I've tried a sample page and its working see the below code.

//Persisting Data after PostBack try putting the data table in Viewstate and look at the size of

// the Viewstate, by enabling Tracing. If its not too big and less than 1 MB, then use the Viewstate,

// as its more efficient and does not use any server resources.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM