How to read SharePoint GridView with artificial Checkbox

I have a web part that has a gridview control on it.  I get a data table from a library method I wrote and bind it to the grid.  I can either have the grid make the columns with autogeneratcolumns set to true or I can iterate through the data table columns and build the grid columns when the page loads.  All that works fine.

I need to add a fake checkbox column and detect which row is checked in a button click and retrieve a column of data from that checked row.   I added a checkbox column to the grid in the designer and it is a template column with an edit and  item template.  I cannot seem to tell if it is checked.

I tried something like the following:
   //Iterate through the Rows property
            foreach (GridViewRow row in this.GridView1.Rows)
            {
                // Access the CheckBox
                //tried that too ==> CheckBox cb = (CheckBox)row.FindControl("CheckBox1");
                CheckBox cb = row.Cells[0].Controls[1] as CheckBox;
                if (cb != null)
                {
                    if (cb.Checked)
                    {
                        acntList.Add("Some Account");
                        return acntList;
                    }
                }
            }

Any ideas on this would be greatly appreciated.
LVL 1
Howard BashSenior Software EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Howard BashSenior Software EngineerAuthor Commented:
Ok, I made a change and in the Page Load I check if it is a post back and only load the datagrid if it not.
Here is the simple fragment with the test condition added:
                if (!Page.IsPostBack)
                {
                    this.GridView1.AutoGenerateColumns = true;
                    this.GridView1.AllowPaging = true;
                    this.GridView1.PageSize = 20;

                    this.GridView1.DataSource = dt.DefaultView;
                    this.GridView1.DataBind();
                }

Now I can see the values and use them.  Here is that piece which is used on a button click to get data for further processing within the web part:
        /// <summary>
        /// Routine used for Debug Only
        /// </summary>
        void ReadAllGridValues()
        {
            string val = string.Empty;

            try
            {
                foreach (GridViewRow Item in this.GridView1.Rows)
                {
                    CheckBox cb2 = (CheckBox)Item.FindControl("chkRead");
                    //cb.Checked
                    if (cb2.Checked)
                    {
                        String Source = Item.Cells[1].Text;
                        String FirstName = Item.Cells[2].Text;
                        String LastName = Item.Cells[3].Text;
                        String FriendlyName = Item.Cells[4].Text;
                        String Email = Item.Cells[5].Text;
                        String Account = Item.Cells[6].Text;
                    }
                }

            }
            catch (Exception ex)
            {
                string emsg = ex.Message.ToString();
                emsg += "||" + ex.StackTrace.ToString();
            }
        }

This works fine "unless" I do a page refresh and click the button again.  At that point I get an "Index" out of range message.  I know I am missing something subtle/trivial here.  I would remove the check for post back but then I don't see the data to use.

Please advise.  I am close but not quite there yet and a bit of wisdom to steer here would be great.
0
Howard BashSenior Software EngineerAuthor Commented:
Found the issue.  I had an extraneous autogeneratecolumns statement outside that do post check and that was messing up the grid by telling it to auto gen columns but not supplying the data table source.  

So,  all is well and I will close this off.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SharePoint

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.