如何准确地将已检查的行从gridview1传输到gridview2(两者都在同一页面中)

时间:2017-05-02 09:03:01

标签: asp.net gridview

目前这个方法在gridview2中只显示gridview1复选框中检查的行的第一列,但我需要将所有列完全显示为gridview1,那么我该怎么做呢?

protected void Button1_Click2(object sender, EventArgs e)
{
List<string> lista = new List<string>();

    for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            CheckBox chbox = GridView1.Rows[i].Cells[0].FindControl("chk") as CheckBox;
            if (chbox.Checked == true)
        {
            lista.Add("'" + GridView1.Rows[i].Cells[1].Text + "'");
        }   
        }   

    DataTable dt = new DataTable();

    GridView2.DataSource = lista;
    GridView2.DataBind();
}

1 个答案:

答案 0 :(得分:0)

按钮的代码将位于

之下

还创建一个将存储gridview1

详细信息的类
protected void Button1_Click2(object sender, EventArgs e)
        {
            List<CopyGrid> lista = new List<CopyGrid>();

            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                CheckBox chbox = GridView1.Rows[i].Cells[0].FindControl("chk") as CheckBox;
                if (chbox.Checked == true)
                {
                    CopyGrid content = new CopyGrid();
                    content.Name = GridView1.Rows[i].Cells[1].Text;
                    content.Email = GridView1.Rows[i].Cells[2].Text;
                    content.City = GridView1.Rows[i].Cells[3].Text;
                    content.PostCode = GridView1.Rows[i].Cells[4].Text;
                    lista.Add(content);
                }
            }

            //     DataTable dt = new DataTable();

            GridView2.DataSource = lista;
            GridView2.DataBind();
        }

这只是您必须实现自己代码的示例代码。