GridView Truncate Heading

Posted on 2006-03-21
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.
Question by:mpdalton
    LVL 25

    Accepted Solution

    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

    LVL 3

    Author Comment

    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.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now