Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a gridview with rows Product Code, Product Name, Category, etc. along with checkboxes in each row. I need a way so that when I click a button named "Add", the Product names along with the Product codes of the ckecked rows of the gridview should be populated into a listbox.

The codes are here:

GetItems.aspx


C#
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage_ShoppingCart.master" AutoEventWireup="true" CodeFile="GetItems.aspx.cs" Inherits="GetItems" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    <style type="text/css">
        .style46
        {
            width: 797px;
        }
        .style47
        {}
        .style48
        {}
        .style49
        {}
        .style50
        {}
    </style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:Panel ID="Panel_SelectItems" runat="server">
    <table class="style42">
        <tr>
            <td class="style46" valign="top">
                <asp:GridView ID="GridView1" runat="server" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
    AutoGenerateColumns="False" CssClass="style47" Width="975px">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:CheckBox ID="chkRow" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="pcode" HeaderText="Product Code" 
            ItemStyle-Width="150" >
        <ItemStyle Width="150px" />
        </asp:BoundField>
        <asp:BoundField DataField="pname" HeaderText="Product Name" 
            ItemStyle-Width="300" >
        <ItemStyle Width="300px" />
        </asp:BoundField>
        <asp:BoundField DataField="pbrand" HeaderText="Brand Name" 
            ItemStyle-Width="300" >
        <ItemStyle Width="300px" />
        </asp:BoundField>
        <asp:BoundField DataField="category" HeaderText="Product Category" 
            ItemStyle-Width="150" >
        <ItemStyle Width="150px" />
        </asp:BoundField>
        <asp:BoundField DataField="price" HeaderText="Price/Quantity" 
            ItemStyle-Width="150" >
        <ItemStyle Width="150px" />
        </asp:BoundField>
        <asp:BoundField DataField="qty" HeaderText="Quantity Available" 
            ItemStyle-Width="100" >
        <ItemStyle Width="100px" />
        </asp:BoundField>
    </Columns>
                    <HeaderStyle BackColor="#3AC0F2" ForeColor="White" />
</asp:GridView>
                <asp:Button ID="Button_Add2Cart" runat="server" CssClass="style49" 
                    Height="58px" onclick="Button_Add2Cart_Click" Text="Add" Width="78px" />
                
                <asp:Button ID="Button1" runat="server" CssClass="style50" Height="58px" 
                    Text="Remove" />
                <br />
                <asp:ListBox ID="ListBox1" runat="server" CssClass="style48" Height="74px" 
                    Width="471px"></asp:ListBox>
                <br />
                
<br />
                <br />
            </td>
        </tr>
    </table>
</asp:Panel>
</asp:Content>

And here is the code-behind: GetItems.aspx.cs

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class GetItems : System.Web.UI.Page
{
    ClassShopCartCon_Rakesh con = new ClassShopCartCon_Rakesh();
    DataSet ds = new DataSet();

    protected void Page_Load(object sender, EventArgs e)
    {
        ds = con.dataFetch("select pcode, pname, pbrand, category, price, qty from products");
        if (!this.IsPostBack)
        {
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
    }
}
Posted

1 solution

Study this example and adapt it to your need:
the aspx page:
XML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default30.aspx.cs" Inherits="Default30" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:GridView ID="GridView1" runat="server">
        <Columns>
            <asp:TemplateField>
                <HeaderTemplate>
                        <asp:Label ID="Label1" runat="server" Text="Check"></asp:Label>
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    <asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    </form>
</body>
</html>


the code behind:
using System;
using System.Web.UI.WebControls;
using System.Data;

public partial class Default30 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == true)
            return;
        DataTable dtTemp = new DataTable();
        dtTemp.Columns.Add("Name");
        dtTemp.Rows.Add("Peter");
        dtTemp.Rows.Add("James");
        dtTemp.Rows.Add("Mick");
        dtTemp.Rows.Add("MAry");
        GridView1.DataSource = dtTemp;
        GridView1.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            CheckBox chkBox = (CheckBox)row.FindControl("CheckBox1");
            if (chkBox != null && chkBox.Checked)
            {
                ListBox1.Items.Add(row.Cells[1].Text.ToString());
                chkBox.Checked = false;
            }
        }
    }
}
 
Share this answer
 
v2
Comments
J. Chatterjee 5-May-14 3:12am    
ok, what is the syntax to populate data table with data from database using sql command?
Peter Leow 5-May-14 3:54am    
Check this out:
http://stackoverflow.com/questions/3888810/populate-datatable-with-records-from-database
J. Chatterjee 5-May-14 3:58am    
Thanks a lot. It worked.. :) :)
J. Chatterjee 5-May-14 4:04am    
Ok, Now I want to change the Column-Headers along with this.. :P :P Please help.. And what is the syntax to clear the listbox
Peter Leow 5-May-14 4:12am    
This is a new question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900