Why am I getting column index out of range error at RowDataBound event?

Hi, I'm using vs2013, asp.net web form, and C#
I want to set a couple of gridview contro's columns' width at run time because the gridview data source is bound at run time so I can't use the smart tag of the control to pre-configure the style of each column, or can I?  Anyway, I'm getting out of range exception when I set the column 7 width. Shouldn't all the columns be created already at this event?  There are a total of 8 columns, the first one is a command button.  When I look at the property of the gridview in debug and it shows only 1 columns is available.  At which event can I set the column width?  Thank you.

        protected void grid_RowDataBound(object sender, GridViewRowEventArgs e)
            grid.Columns[7].ItemStyle.Width = Unit.Percentage(10.0);
            grid.Columns[7].ItemStyle.Wrap = true;
            grid.Columns[8].ItemStyle.Width = Unit.Percentage(10.0);
            grid.Columns[8].ItemStyle.Wrap = true;
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

AndyAinscowFreelance programmer / ConsultantCommented:
>>There are a total of 8 columns

grid.Columns[0] is the first, grid.Columns[1] is the second... grid.Columns[7] is the final one and grid.Columns[8] is out of range.
AndyAinscowFreelance programmer / ConsultantCommented:
ps.  You might want to look at this example from microsoft, it uses an extra check before attempting to set cell contents - I don't know if that is relevant.

pps.  You have set the autogeneratecolumns="true" for the gridview I hope.

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
lapuccaAuthor Commented:
Hi, I have total of 9 columns.  I changed to the following code and it make no difference.  The gridview control overflow way to the right of my screen and out of the body container class width of 1170 pixels.  any suggestions to fix this?  Thank you.

        protected void grid_RowDataBound(object sender, GridViewRowEventArgs e)

            if (e.Row.RowType == DataControlRowType.Header)

                e.Row.Cells[0].Width = new Unit("5%");
                e.Row.Cells[1].Width = new Unit("7%");
                e.Row.Cells[2].Width = new Unit("7%");
                e.Row.Cells[3].Width = new Unit("7%");
                e.Row.Cells[4].Width = new Unit("7%");
                e.Row.Cells[5].Width = new Unit("7%");
                e.Row.Cells[6].Width = new Unit("7%");
                e.Row.Cells[7].Width = new Unit("20%");
                e.Row.Cells[8].Width = new Unit("20%");

                //gridIrb.Columns[7].ItemStyle.Width = Unit.Percentage(10.0);
                //gridIrb.Columns[7].ItemStyle.Wrap = true;
                //gridIrb.Columns[8].ItemStyle.Width = Unit.Percentage(10.0);
                //gridIrb.Columns[8].ItemStyle.Wrap = true;

AndyAinscowFreelance programmer / ConsultantCommented:
>>I changed to the following code and it make no difference.  The gridview control overflow way to the right of my screen and out of the body container class width of 1170 pixels.

How can you see the gridview if it errors with an index out of range?  If the index out of range is solved then please close this question.  Not fitting on screen is totally different.
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

From novice to tech pro — start learning today.