Hi itsjayshah,
I have created sample that full-fill your requirement.
So you need to change the code as per your requirement.
HTML
<asp:GridView runat="server" ID="gvprodet" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="product_id" HeaderText="" />
<asp:BoundField DataField="Size" HeaderText="Size" />
<asp:BoundField DataField="Color" HeaderText="Color" />
<asp:BoundField DataField="Quantity" HeaderText="Quantity" />
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:Button Text="Save" runat="server" OnClick="Save" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Label ID="lblCookieValue" runat="server" />
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dtprodet = new DataTable();
dtprodet.Columns.AddRange(new DataColumn[4] {
new DataColumn("product_id", typeof(int)), new DataColumn("Size", typeof(int)),
new DataColumn("Color",typeof(string)),new DataColumn("Quantity", typeof(int)) });
dtprodet.Rows.Add(1, 6, "Red", 10);
dtprodet.Rows.Add(2, 7, "Blue", 5);
dtprodet.Rows.Add(3, 8, "Orange", 8);
dtprodet.Rows.Add(4, 9, "Green", 3);
gvprodet.DataSource = dtprodet;
gvprodet.DataBind();
}
}
protected void Save(object sender, EventArgs e)
{
Button btn = sender as Button;
GridViewRow row = btn.NamingContainer as GridViewRow;
string product_id = row.Cells[0].Text.Trim();
string size = row.Cells[1].Text.Trim();
string color = row.Cells[2].Text.Trim();
string quantity = row.Cells[3].Text.Trim();
if (Request.Cookies["prodet"] != null)
{
Response.Cookies["prodet"]["product_id"] = Request.Cookies["prodet"]["product_id"] += "," + product_id;
Response.Cookies["prodet"]["Size"] = Request.Cookies["prodet"]["Size"] += "," + size;
Response.Cookies["prodet"]["Color"] = Request.Cookies["prodet"]["Color"] += "," + color;
Response.Cookies["prodet"]["Quantity"] = Request.Cookies["prodet"]["Quantity"] += "," + quantity;
Response.Cookies["prodet"].Expires = DateTime.Now.AddDays(1);
lblCookieValue.Text = Request.Cookies["prodet"].Value;
}
else
{
Response.Cookies["prodet"]["product_id"] = product_id;
Response.Cookies["prodet"]["Size"] = size;
Response.Cookies["prodet"]["Color"] = color;
Response.Cookies["prodet"]["Quantity"] = quantity;
Response.Cookies["prodet"].Expires = DateTime.Now.AddDays(1);
lblCookieValue.Text = Request.Cookies["prodet"].Value;
}
}
Screenshot
![](https://i.imgur.com/x22FeB1.gif)