Link to home
Start Free TrialLog in
Avatar of lapucca
lapucca

asked on

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;
        }
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

>>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.
ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lapucca
lapucca

ASKER

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;



        }
>>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.