• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 12293
  • Last Modified:

GridView Column Width

we have a gridview that we are manually setting up the columns in.

the column widths are defined by the user, but occasionly they specify a width that is smaller than required for the heading text.

the gridview automatically resizes the column and makes the other columns slightly smaller to fix this up.

the problem is that we dynamically add controls above and below the gridview to filtering and totalling so when the gridview is automatically resized the controls dont match up.

when we ask the column width, even after calling the databind method, it returns the width we set it as, not what it has been resized too.

how do i get the real column width.

note: when i talk about columns, i'm referrring to DataControlField's which are what dataview's call columns.
1 Solution
How is width set? By %? What are default settings for all columns (% or px)?
mpdaltonAuthor Commented:
they are in px

Dim dgc As DataGridColumnStyle
   dgc = DataGrid1.TableStyles(0).GridColumnStyles("id")
   dgc.Width = 500
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

i suspect that what is happening is the browser is doing the 'resizing' to fit its contents. The code that your aspx page produces will have the correct column width for each column - but the browser needs to enlarge the column to fit it all in.

look at the page source of a page with problems and see what the column width is. this will tell you if its the aspx or the browser doing the resizing.
mpdaltonAuthor Commented:
hi solar_flare, you are right, the html is correct when i view the source, so it's the brower that is causing the problem.

any ideas how to override this behaviour so it maybe auto truncates the field or wraps it ? the column heading is already set to wrap but this only works when there are multiple words to a heading not one long word.
browsers would not be capable of truncating or wahtever, you would have to rename the columns so they are short enought to fit or change the font size

for each c as datacolumn in yourDatatable.Columns
    if c.ColumnName.Length > 10 then c.columnName = c.ColumnName.Substring(0, 10)


something like that in your codebehind would stop the headings being too long, butmight make it look ugly?

maybe resize the font of the header based on the length of the longest word so it wraps correctly? this would be more coding but might look better.
mpdaltonAuthor Commented:
thanks for your help solar_flare. now that i know the limitiations of the browser i can let the project sponsor know that users will just have to check during table setup that the field width is wide enough.

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.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now