?
Solved

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

Posted on 2016-08-29
6
Medium Priority
?
93 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 21

Accepted Solution

by:
Tapan Pattanaik earned 2000 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 2000 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

765 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