I would like to transfer the selected rows from gridview1 to gridview2, but I am able to do only the primary key column into the
second grid.
How to modify the code to have all columns displayed/ is there a better way to do it?
The code:
protected void Button1_Click(object sender, EventArgs e)
{
DataClassesDataContext context = new DataClassesDataContext();
var f = from z in context.Availabilities
join p in context.Items
on z.ItemID equals p.ItemID
join x in context.CDs
on p.ItemID equals x.itemID
join t in context.Artists
on p.ItemID equals t.itemID
group z by new { z.ItemID, t.artistName, p.Price, x.albumName, x.recordingStudio, x.CdID } into g
select new { itemID = g.Key.ItemID, albumName = g.Key.albumName, Artists = g.Key.artistName, recordingStudio = g.Key.recordingStudio, Price = g.Key.Price, totalQty = g.Sum(z => z.Quantity) };
GridView1.DataSource = f;
GridView1.DataBind();
}
protected void Button4_Click1(object sender, EventArgs e)
{
DataClassesDataContext context = new DataClassesDataContext();
var selectedProducts = GridView1.Rows.Cast<GridViewRow>()
.Where(row => ((CheckBox)row.FindControl("SelectedProducts")).Checked)
.Select(row => GridView1.DataKeys[row.RowIndex].Value.ToString()).ToList();
if (Session["Cart"] == null)
{
Session["Cart"] = selectedProducts;
}
else
{
var cart = (List<string>)Session["Cart"];
foreach (var product in selectedProducts)
cart.Add(product);
Session["Cart"] = cart;
}
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox cb = (CheckBox)row.FindControl("SelectedProducts");
if (cb.Checked)
cb.Checked = false;
}
GridView2.DataSource = selectedProducts;
GridView2.DataBind();
}