我的GridView1的结果会在事件触发后发生变化。 我需要从GridView1 追加数据(不是复制)到我的GridView并保存GridView1中的所有结果。
这是我的代码(不完整)和我的目标:
_site
└── animals
├── index.html
├── mammals
│ ├── cats
│ │ ├── housecat.html
│ │ └── tiger.html
│ ├── dogs
│ │ ├── doberman.html
│ │ └── poodle.html
│ └── index.html
└── reptiles
└── lizards
├── chameleon.html
└── iguana.html
更新:
这是完整的解决方案:
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();
}
答案 0 :(得分:1)
当您设置DataSource是服务器端事件时,页面再次呈现并且每行都被创建,因此您将附加要绑定的DataTable。 当前的DataSource应保存在Session
中Session["currentDataSet"] = dataTableName;
dataTableNew.Merge((DataTable)Session["currentDataSet"]);
GridView2.DataSource = dataTableNew;
GridView2.DataBind();
答案 1 :(得分:0)
如果您创建了以下内容:
来自gridView2 dataSetGridView2的数据集
GridView2.DataSource = dataSetGridView1.Merge(dataSetGridView2);
答案 2 :(得分:0)
将数据从gridview1复制到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();
}