How can I set the width of a column in a GridView

Hi Experts,
I am using a bound gridview in my ASP.Net application with C# codebehind.  I use the code below to bind the grid, but I need to reset the column width how can I do this?

            gdvAddresses.DataSource = dt;
            gdvAddresses.Visible = true;
            gdvAddresses.DataBind();

Thanks in advance,
mrotor
mainrotorAsked:
Who is Participating?

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

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

asp_net2Commented:
you can set the width within the GridView control. See below for an example.

<asp:GridView Width="100%" ID="gvCustomers" AutoGenerateColumns="False" runat="server" />

you can also set the width using px rather than % in the example above.

Hope this helps..
0
Roshan DavisCommented:
How to: Set GridView Web Server Control Column Width Dynamically - http://msdn.microsoft.com/en-us/library/ms178296(VS.80).aspx
GridView1.Columns[i].ItemStyle.Width = colWidth;

Open in new window

0
mainrotorAuthor Commented:
I am trying to set the width using the following code but I get the following exception:

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.


Here's my code:
gdvAddresses.Columns[1].ItemStyle.Width = 200;

What's wrong with this code?

Thanks in advance
mrotor
0
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Roshan DavisCommented:
if it is the first column, you should use ZERO as the index.

If it is more than one column, MAKE SURE THAT you are using the statement after ADDING THE COLUMNS.
gdvAddresses.Columns[0].ItemStyle.Width = 200;

Open in new window

0
mainrotorAuthor Commented:
Hi all,
This is my current code.  I reset the column width after I bind the grid.  Could that be causing the problem?  Thanks in advance.

            gdvAddresses.DataSource = dt;
            gdvAddresses.Visible = true;
            gdvAddresses.DataBind();
            gdvAddresses.Rows[0].Enabled = true;

            SizeGrid();

        protected void SizeGrid()
        {
            gdvAddresses.Columns[1].ItemStyle.Width = 200;
            gdvAddresses.Columns[2].ItemStyle.Width = 200;
        }
0
Roshan DavisCommented:
make sure gdvAddresses.Columns[1].ItemStyle has any property AutoSizeMode, if yes set that to the proper value.
0
mainrotorAuthor Commented:
The gridview properties look like the image attached to this reply.
Gridview.bmp
0
asp_net2Commented:
You will need to add the width to the ItemStyle to your appropriate Filed listed above for Fields.

Example: If you have a BoundField Field then you need to select that Field on the left and then choose the width for the ItemStyle for the Field properties.

Hope this helps!!
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
.NET Programming

From novice to tech pro — start learning today.