Hello,
I am designing a item cart in asp.net C#.
On ADD TO CART button item add from Datalist to Gridview. Now what i do, if Item already exists in grdiview than item quanity increase by 1. Please help in c# Code.
ASPX Code-
<asp:DataList ID="dlemp" runat="server" DataKeyField="Id" Width="670px" RepeatDirection="Horizontal" CellPadding="2" Font-Names="Times New Roman" ForeColor="Black" RepeatColumns="3"> <%-- <AlternatingItemStyle BackColor="white" /> <FooterStyle BackColor="Tan" /> <HeaderStyle BackColor="Tan" Font-Bold="True" />--%> <ItemTemplate> <asp:Panel ID="Panel1" runat="server" BorderColor="white" BorderWidth="2px" CssClass="marg" BackColor="White" Height="250px" Width="210px"> <div style="text-align: center; width: 210px;"> <br /> <asp:Image ID="imgEmp" Height="100px" Width="100px" runat="server" ImageUrl='<%# "data:image/jpg;base64," + Convert.ToBase64String((byte[])Eval("Image")) %>' /> <div style="height: 12px;"></div> <div style="text-align: center;"> <asp:Label ID="lblProductName" runat="server" ForeColor="black" Font-Size="14px" Text='<%# Eval("ImageName") %>'></asp:Label> </div> <div style="text-align: center; color: black;"> ? <asp:Label ID="Label2" runat="server" ForeColor="black" Font-Size="14px" Text='<%# Eval("Price") %>'></asp:Label> </div> <div style="height: 10px;"></div> <asp:Label Visible="false" ID="ItemID" runat="server" Text='<%# Eval("Id") %>'></asp:Label> <asp:Button ID="btnadd" runat="server" Width="90px" Font-Size="12px" Text="Add To Cart" OnClick="AddToCart"></asp:Button> </div> <br /> </asp:Panel> </ItemTemplate> <SelectedItemStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" /> </asp:DataList>
//GRIDVIEW
<asp:GridView ID="GridView1" runat="server" ShowHeader="false" ForeColor="Darkblue" EmptyDataText="Cart is Empty" Font-Size="13px" AutoGenerateColumns="false"> <Columns> <asp:TemplateField> <ItemTemplate> <asp:Label ID="lblImageN" runat="server" Width="120px" Text='<%#Eval("ImageName")%>' Style="text-align: left; padding-left: 4px;"></asp:Label> </ItemTemplate> </asp:TemplateField> <%-- <asp:BoundField DataField="ImageName" HeaderText="Item" ItemStyle-HorizontalAlign="left" ItemStyle-Width="120px" />--%> <asp:BoundField DataField="Price" HeaderText="Price" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="40px" ItemStyle-CssClass="price" /> <asp:TemplateField HeaderText="Quantity"> <ItemTemplate> <div style="height: 2px;"></div> <asp:Button ID="btn1" runat="server" Text=" - " Height="18px" CssClass="Minus" /> <asp:TextBox ID="txtQuantity" runat="server" onblur="Calculation(this.value)" Width="28" Height="18px" Style="padding-top: 4px;"></asp:TextBox> <asp:Button ID="btn2" runat="server" Text=" + " Height="18px" CssClass="Plus" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Total"> <ItemTemplate> <asp:Label ID="lblTotal" runat="server" Width="67px" Text="0" Style="text-align: right; padding-right: 8px;"></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> <EmptyDataTemplate>No Record Available</EmptyDataTemplate> </asp:GridView>
C# Code
protected void AddToCart(object sender, EventArgs e) { string id = ""; Button btn = (Button)sender; DataListItem item = (DataListItem)btn.NamingContainer; id += (item.FindControl("ItemID") as Label).Text; string query = "SELECT * FROM tablename where Id IN(" + id.TrimEnd(',') + ")"; DataTable dt = GetData(query); if (Session["dt"] == null) { Session["dt"] = dt; GridView1.DataSource = dt; GridView1.DataBind(); lblTotalItems.Text = "" + (GridView1.DataSource as DataTable).Rows.Count; } else { DataTable dt1 = Session["dt"] as DataTable; for (int i = 0; i < dt.Rows.Count; i++) { dt1.Rows.Add(dt.Rows[i][0], dt.Rows[i][1], dt.Rows[i][2], dt.Rows[i][3]); } GridView1.DataSource = dt1; lblTotalItems.Text = " " + (GridView1.DataSource as DataTable).Rows.Count; GridView1.DataBind(); } }