Vova Potapov Vova Potapov - 5 months ago 11
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];

//my goal
private void appendToGridView2 ()
//i need some think like this


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;


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;
GridView2.DataSource = dataTableNew;

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