1

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

2
  • 2
    You are re-creating the DataTable every time, thus you are able to see only the current row. Commented Jan 2, 2015 at 5:05
  • Hi, I have tried creating the table at the main once and pass it to "addPlanDetailDataRow(dt)" but somehow it won't work Commented Jan 2, 2015 at 5:08

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, i have tried calling InitDataTable() during the Page_Load under the if (!ispostback) But it's doesn't work
You are welcome, you may add the asp.net tag in your question to make it clear that you are working on web forms.
3

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

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.