Check box

I have a datagridview in c# winforms. In my procedure I am trying to check all rows in the grid. There are two columns in the grid: first column is a check box column and the send one is a regular one. For some reason it does not check the very first row check box in the grid when I run my select all check boxes procedure. Please let me know if it is fixable because to me it looks like a glitch in the grid.
LeopoledAsked:
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.

Ess KayEntrapenuerCommented:
you are starting your loop from 1,

you need to start the loop from ZERO
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
LeopoledAuthor Commented:
No I am starting from 0, here is my code:
int keyreclistpos = 0;
            long ii = 0;        

            //test

            keyreclstbx.Visible = false;
            printcmd.Enabled = false;
            System.Windows.Forms.Application.DoEvents();
            keyreclistcnt = keyreclstbx.RowCount;
            if (selectcmd.Text == "SELECT ALL")
            {
                keyreclstbx.ClearSelection();
                foreach (DataGridViewRow row in keyreclstbx.Rows)
                {
                    if (keyreclistpos == 0)
                    {
                        row.Cells[0].Value = true;
                        row.Cells[0].Value = 1;
                        row.Cells[0].Selected = true;
                        keyreclstbx.CurrentCell = row.Cells[0];
                        keyreclstbx[0, keyreclistpos].Selected = true; // added by gregory libo, 09-10-2013
                        keyreclistpos = keyreclistpos + 1;
                    }
                    else
                    {
                        row.Cells[0].Value = true;
                        row.Cells[0].Selected = true;
                        //keyreclstbx.CurrentCell = row.Cells[0];
                        //keyreclistpos = keyreclistpos + 1;
                    }
                    //row.Cells[0].Value = true;
                    //row.Cells[0].Selected = true;
                    //keyreclstbx.CurrentCell = row.Cells[0];
                    //keyreclistpos = keyreclistpos + 1;
                }

                try
                {
                    keyreclstbx.CurrentCell = keyreclstbx[0, 1];
                }
                catch (Exception)
                {
                   
                }
               

                //keyreclstbx_Leave(keyreclstbx, null);
               

                selectcmd.Text = "UN-SELECT ALL";
            }
            else
            {
                keyreclistcnt = keyreclstbx.RowCount;
                keyreclistcnt = keyreclistcnt - 1;
                keyreclistpos = 0;

                foreach (DataGridViewRow row in keyreclstbx.Rows)
                {
                    row.Cells[0].Value = false;
                    //row.Cells[0].Value = 1;
                    row.Cells[0].Selected = false;
                    keyreclstbx.CurrentCell = row.Cells[0];
                }

                try
                {
                    keyreclstbx.CurrentCell = keyreclstbx[0, 1];
                }
                catch (Exception)
                {

                }                
                selectcmd.Text = "SELECT ALL";
            }
            keyreclstbx.Visible = true;
            printcmd.Enabled = true;
            keyreclstbx.Focus();
0
Ess KayEntrapenuerCommented:
it checks it


change to this


            long ii = 0;        

            //test

            keyreclstbx.Visible = false;
            printcmd.Enabled = false;
            System.Windows.Forms.Application.DoEvents();
            keyreclistcnt = keyreclstbx.RowCount;
            if (selectcmd.Text == "SELECT ALL")
            {
                keyreclstbx.ClearSelection();
                foreach (DataGridViewRow row in keyreclstbx.Rows)
                {
                   
                        row.Cells[0].Value = true;
                        row.Cells[0].Selected = true;
                   
                }

                try
                {
                    keyreclstbx.CurrentCell = keyreclstbx[0, 1];
                }
                catch (Exception)
                {
                    //do nothing
                }
             selectcmd.Text = "UN-SELECT ALL";
            }
            else
            {
                keyreclistcnt = keyreclstbx.RowCount;
                keyreclistcnt = keyreclistcnt - 1;
              foreach (DataGridViewRow row in keyreclstbx.Rows)
                {
                    row.Cells[0].Value = false;
                    row.Cells[0].Selected = false;
                    keyreclstbx.CurrentCell = row.Cells[0];
                }

                try
                {
                    keyreclstbx.CurrentCell = keyreclstbx[0, 1];
                }
                catch (Exception)
                {

                }                
                selectcmd.Text = "SELECT ALL";
            }
            keyreclstbx.Visible = true;
            printcmd.Enabled = true;
            keyreclstbx.Focus();

Open in new window

0
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 Development

From novice to tech pro — start learning today.