Solved

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

Posted on 2016-08-29
6
55 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
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

777 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