Vova Potapov Vova Potapov - 3 months ago 7
ASP.NET (C#) Question

How to append data from one GridView to second GridView?

I have GridView1 with results that change after my event fire.
I need to append data (not copy) from GridView1 to my GridView and save all results that was in GridView1.

Here is my code(not full) and my goal:

private void BindQuestions(int quiz)
{
//i have fuction that run this each time with difrant id
//and GridView1 thatshows me result each time
DataSet oDs = SqlHelper.ExecuteDataset(sCon, "Ps_Quiz_GetQsns",quiz));
GridView1.DataSource = oDs.Tables[0];
GridView1.DataBind();
}

//my goal
private void appendToGridView2 ()
{
//i need some think like this
GridView2.DataSource.append(GridView1.DataSource);
GridView2.DataBind();
}


UPDATE:

This is full solution:

bool DoMerge =false//this is global and static
if (Session["currentDataSet"] == null)
{
Session["currentDataSet"] = SqlHelper.ExecuteDataset(sCon, "Ps_Quiz_GetQsns", Int32.Parse(ddlQuestionTypeName.SelectedValue), Int32.Parse(ddlQuestionSubTypeName.SelectedValue)); ;
}
QuestionsCollection = (DataSet)(Session["currentDataSet"]);
if (DoMerge) QuestionsCollection.Merge(SqlHelper.ExecuteDataset(sCon, "Ps_Quiz_GetQsns",quiz);
DoMerge = true;
Session["currentDataSet"] = QuestionsCollection;
GridViewAllQuestions.DataSource = QuestionsCollection;
GridViewAllQuestions.DataBind()

Answer

When you set DataSource which is Server Side event, the Page renders again and each row is created, so instead you append the DataTables which you are binding. The current DataSource should be saved in Session

Session["currentDataSet"] = dataTableName;
dataTableNew.Merge((DataTable)Session["currentDataSet"]);
GridView2.DataSource = dataTableNew;
GridView2.DataBind();

Below will provide some help: How to append one DataTable to another DataTable