We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now


Gridview Boundfield and checkbox

Medium Priority
Last Modified: 2013-11-26
I have a gridview that is populated from a database.  The gridview has a checkbox in an ItemTemplate field.  I want to loop through the gridview, find the rows that have been checked, extract the value from a boundfield in the gridview and pass this value to a stored procedure that updates the database.

I have the code to loop through the gridview to see if the row is check and the code to update the database, however, I need the correct code to extract the value from the boundfield.  I tried using something similar to "TextBox2.Text = GridView2.SelectedRow.Cells(2).Text", however, I receive an error message because the row is not selected.

Any assistance would be greatly appreciated.
Watch Question

Unlock this solution and get a sample of our free trial.
(No credit card required)
Unlock this solution and get a sample of our free trial.
(No credit card required)

I changed your GridView around just slightly to accomodate my devTable, but the layout is the same. This should give you the idea of how to get at the data your looking at (see snippet).
****ASPX CODE****
<!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 runat="server">
    <title>Untitled Page</title>
    <form id="form1" runat="server">
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
                     <asp:CheckBox runat="server" ID="chkSelected"/>
               <asp:BoundField DataField="testid" HeaderText="ID" />
               <asp:BoundField DataField="fName" HeaderText="First Name" />
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" />
        <span>Checked Row Values Here: </span>
        <asp:TextBox ID="txtResults" runat="server" />
    private string connStr
        get { return ConfigurationManager.ConnectionStrings["conn"].ToString(); }
    protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack) bindToGrid();
    protected void Button1_Click(object sender, EventArgs e)
        foreach (GridViewRow gRow in GridView1.Rows)
            CheckBox rb = (CheckBox)gRow.Cells[0].Controls[1];
            if (rb.Checked)
                txtResults.Text += "[ " + gRow.Cells[1].Text + "/" + gRow.Cells[2].Text + " ] - ";
        //being lazy or I would remove the ending "-" from txtResults above code causes.
    private void bindToGrid()
        SqlConnection conn = new SqlConnection(connStr);
        SqlCommand cmd = new SqlCommand("SELECT * FROM DevTable", conn);
        SqlDataReader dr = cmd.ExecuteReader();
        GridView1.DataSource = dr;

Open in new window

Looks like Jinal and I have the same idea :)

Only thing is I would try not to use "FindControl" if I didn't have to...you pay a penalty for using that too often in a large project, and it should be avoided.

ha I'm looking at that and I thought akajt had posted that second posting (which jinal did) lol...I took first half of jinal's code and used it as a template for my post....for shame! Ah well give him the points if you award them...my bad.

Hello akajt,

Can you please look at my comment with ID:24819654 ? This is the same as  accepted solution.

You can contact moderator to chnage point if you wish.



Hello jinal,

The two responses are similar, however, I used the solution from ddayx10 because it didn't use the FindControl.  I have no problem with changing the assigned points.


You can contact moderator by request attention in question section on this page.

You can see that even other person agree that he did use my part of code for his comment.

Thanks and waiting for your response.


When I click on 'Request Attention' I receive the message:

This question has already been reported and may not be reported again so soon.
An Advisor will attend to the question shortly.

Apparently, someone else has an issue with the points that I assigned.  When I am contacted by the moderator, I will split the points.  I apologize if assigning the points to ddayx10 caused you to feel slighted.  This was not my intent.  I just found his code easier to follow.

I did feel bad about that jinal. It was an honest mistake. It doesn't just happen to you though....

happened to me here:

I just go by the posting in the FAQ that states:
"If the question has been closed by the Asker, and he has awarded the points to another Expert, you can click the Request Attention button, but unless there is an obviously serious error, the Moderators will tend to accept the decision of the Asker; after all, she/he knows what worked for her/him."

I will say that...
a) although our code was similar I did not copy 1 line from your code behind (even though we took the same approach to the loop, anyone who knows what they are doing would take that approach)
b) the user did end up using my code as opposed to yours
c) I apologized right after I saw my mistake, and still think you deserve the points, but maybe not all since you used FindControl in such a way that was completely unnecessary. I should get like 1 point for quality control  :)

Hello ddayx10,
It is really a nice that you explain your point . I also agree that points should split between us.


Jinal 499pt
Dday 1pt

LOL...you really did get there first pal points are not important in this context.


Although I did use the code from ddayx10, he wishes to have the points assigned to jinal.  Thanks to you both.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.