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?
 
asp_net2Connect With a Mentor Commented:
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
 
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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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

All Courses

From novice to tech pro — start learning today.