A junior developer asked me can I select some rows from a GridView and copy them to another GridView?
I told him sure all you have to do is to create a DataTable and the columns that will be added to it.
Now you will loop for each row in the first GridView then add the selected rows to the DataTable.
Set the DataSource of the other GirdView to DataTable.
Here is the code that would do the trick:
I told him sure all you have to do is to create a DataTable and the columns that will be added to it.
Now you will loop for each row in the first GridView then add the selected rows to the DataTable.
Set the DataSource of the other GirdView to DataTable.
Here is the code that would do the trick:
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("FirstName"));
dt.Columns.Add(new DataColumn("LastName"));
foreach (GridViewRow row in GridViewEmployees.Rows)
{
if(((CheckBox)row.Cells[3].FindControl("CheckBox")).Checked == true)
{
dr = dt.NewRow();
dr["ColumnName1"] = ((Label)row.Cells[0].FindControl("Label")).Text;
dr["ColumnName2"] = ((Label)row.Cells[1].FindControl("Label")).Text;
dt.Rows.Add(dr);
}
}
GridViewManagers.DataSource = dt;
GridViewManagers.DataBind();
Source: https://www.nilebits.com/blog/2010/10/how-to-copy-some-rows-from-a-gridview-to-another-in-asp-net/