Solved

In GridView get cell value when check/uncheck CheckBox control for the same row.

Posted on 2016-08-29
6
47 Views
Last Modified: 2016-08-29
Hi:
How could I get the value of a cell(Product ID) in Gridview , when clicking checkbox control on the same row of the gridview?
Please help.
my web form code here:
<body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView ID="GridView1" runat="server"  AllowPaging="True"  PageSize="4" AutoGenerateColumns="False" 
                      >
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True"  />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Product ID">
                    <ItemTemplate>
                        <asp:Label ID="lblProductID" runat="server" Text='<%# Bind("ProductID") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="Name" HeaderText="Name" />
                <asp:BoundField DataField="Price" HeaderText="Price" />
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>

Open in new window

my C# code here
public partial class EEquestion2 : System.Web.UI.Page
    {
        salesDataContext db = new salesDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            BindData();
        }
        private void BindData()
        {
            var myProducts = db.Products;

            if ((myProducts != null) && (myProducts.Count() > 0))
            {
                GridView1.DataSource = myProducts;
                GridView1.DataBind();
            }
        }
    }

Open in new window


my gridview
0
Comment
Question by:Mohammad Alsolaiman
  • 3
  • 3
6 Comments
 
LVL 21

Accepted Solution

by:
Tapan Pattanaik earned 500 total points
ID: 41775226
Hi M_SOLAIMAN,

Logic is like this and for more details please check the below link.
 string Id;
       foreach (GridViewRow gvrow in GridView1.Rows)
            {
                CheckBox chk = (CheckBox)gvrow.FindControl("CheckBox1");
                if (chk != null & chk.Checked)
                {
                    Id = gvrow.Cells[2].Text.ToString();
                }
            }
                

Open in new window



http://www.c-sharpcorner.com/UploadFile/20abe2/get-row-cell-value-from-grid-view-of-checked-checkbox-in-asp/
0
 

Author Comment

by:Mohammad Alsolaiman
ID: 41775237
Please. Where to put this code? In which event.
0
 
LVL 21

Assisted Solution

by:Tapan Pattanaik
Tapan Pattanaik earned 500 total points
ID: 41775246
For Example If you want to get all product ID values in  button click then you have to put it in Button_Click evnt.

For reference you can check the below two links.

CheckBox in Gridview:
http://www.c-sharpcorner.com/uploadfile/syedshakeer/checkbox-in-gridview/

 Get Gridview Row Values When Checkbox Selected in Asp.net
http://www.aspdotnet-suresh.com/2013/03/get-gridview-row-values-when-checkbox.html
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:Mohammad Alsolaiman
ID: 41775253
Yes I like to have the value when checking the checkbox control in the gridview
0
 
LVL 21

Expert Comment

by:Tapan Pattanaik
ID: 41775265
Hi Solaiman,

You got the answer or are u facing any problem !!!
0
 

Author Closing Comment

by:Mohammad Alsolaiman
ID: 41775716
thank u very much for your help.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now