Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. Setting Datagrid column width dynamically

Setting Datagrid column width dynamically

Scheduled Pinned Locked Moved ASP.NET
cssquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Veeves
    wrote on last edited by
    #1

    When a datagrid gets populated, if the data is longer than the header text, the column is only wide enough for the data to fit. The grid lines are right up against the data making it difficult to read. is there a way to manipulate the column width at runtime adding some space between the data and grid lines? Veeves

    N V 2 Replies Last reply
    0
    • V Veeves

      When a datagrid gets populated, if the data is longer than the header text, the column is only wide enough for the data to fit. The grid lines are right up against the data making it difficult to read. is there a way to manipulate the column width at runtime adding some space between the data and grid lines? Veeves

      N Offline
      N Offline
      NeverHeardOfMe
      wrote on last edited by
      #2

      At design time you can set CellPadding and CellSpacing property of DataGrid - remember, the dataGrid renders its results in an HTML table, and these properties simply translate to those of the table. You can set individual ciolumn widths at runtime using, for example: DataGrid1.Columns(2).ItemStyle.Width = System.Web.UI.WebControls.Unit.Percentage(30) This would change the third column to 30% of the overall width. You could use ...Pixel(100) if you wanted to give it a fixed width of, say, 100 pixels instead. cheers Phil

      V 1 Reply Last reply
      0
      • N NeverHeardOfMe

        At design time you can set CellPadding and CellSpacing property of DataGrid - remember, the dataGrid renders its results in an HTML table, and these properties simply translate to those of the table. You can set individual ciolumn widths at runtime using, for example: DataGrid1.Columns(2).ItemStyle.Width = System.Web.UI.WebControls.Unit.Percentage(30) This would change the third column to 30% of the overall width. You could use ...Pixel(100) if you wanted to give it a fixed width of, say, 100 pixels instead. cheers Phil

        V Offline
        V Offline
        Veeves
        wrote on last edited by
        #3

        Thanks for the response. CellPadding and CellSpacing will increase the height of the row which I don`t want. I could fix the width with percentage or pixels, but with variable length data, this method will only work sometimes. What would be best is if there was a way to convert units to a number type and back. Then I could grab the width of a column, add some units to it and then convert it back. Veeves

        1 Reply Last reply
        0
        • V Veeves

          When a datagrid gets populated, if the data is longer than the header text, the column is only wide enough for the data to fit. The grid lines are right up against the data making it difficult to read. is there a way to manipulate the column width at runtime adding some space between the data and grid lines? Veeves

          V Offline
          V Offline
          VaibhavJ
          wrote on last edited by
          #4

          Hi, I think u will have to check for ItemDataBound event of the datagrid for this purpose maybe check if u can get the current column width and then change it there only or adjust the text by inserting
          into the string to be bound to that column... I haven't tried this yet...But just thought of sharing this idea... Thanks for reading.. Thanks & regards, Vaibhav

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups