We help IT Professionals succeed at work.

GridView Truncate Heading

mpdalton asked
Medium Priority
Last Modified: 2008-01-09
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, no what it has been resized too.

how do i get the real column width, or if that isn't possible, could i somehow truncate the heading so it stays the correct width.

note: when i talk about columns, i'm referrring to DataControlField's which are what dataview's call columns.
Watch Question

You need to handle that at Row_Created event. A code snippet is given below but you will need to modify it according to your requirement:

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        int INT_WIDTH_LIMIT = 18;
        if (e.Row.RowType == DataControlRowType.Header)
           if (e.Row.Cells[CELL_INDEX].Text > INT_WIDTH_LIMIT)
              e.Row.Cells[CELL_INDEX].Text  = e.Row.Cells[2].Text.Substring(0,15)+"...";
          //Calculate the width for other Header


Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


thanks for your answer, i've realised after a bit of work that truncating isn't going to work for me as i allow the field to wrap if it has multiple words.

i'll give you the points since your answer was right and reask the question.

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.