Reputation: 2437
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()
Upvotes: 1
Views: 973
Reputation: 2703
What if you create:
DataSet from gridView2 dataSetGridView2
GridView2.DataSource = dataSetGridView1.Merge(dataSetGridView2);
Upvotes: 0
Reputation: 10218
Code to copy data from gridview1 to gridview2
protected void Button1_Click(object sender, EventArgs e)
{
var dt2 = new DataTable();
dt2.Columns.Add("col1", typeof(string));
dt2.Columns.Add("col2", typeof(string));
/....
int row = gv1.Rows.Count;
int col = gv1.Rows[0].Cells.Count;
for (int i = 0; i < row; i++)
{
DataRow rw = dt2.NewRow();
for (int j = 0; j < col; j++)
{
rw[j] = gv1.Rows[i].Cells[j].Text;
}
dt2.Rows.Add(rw);
}
gv2.DataSource = dt2;
gv2.DataBind();
}
Upvotes: 0
Reputation: 1107
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
Upvotes: 1