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
  1. Home
  2. General Programming
  3. C#
  4. How to change Datagrid column width???

How to change Datagrid column width???

Scheduled Pinned Locked Moved C#
helptutorialquestion
2 Posts 2 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.
  • R Offline
    R Offline
    Reinier van de Wetering
    wrote on last edited by
    #1

    I'm trying this code, but I get the error :" Object reference not set to an instance of an object". The code: // Create new Table Style DataGridTableStyle ts = new DataGridTableStyle(); ts.MappingName = "Employees"; this.dataGrid1.TableStyles.Clear(); this.dataGrid1.TableStyles.Add(ts); // Assign New Width to DataGrid column this.dataGrid1.TableStyles["Employees"].GridColumnStyles["EmployeeName"].Width = newwidth; I set the DataSource for the Datagrid to dataset.Employees on the DataGrid Properties. Does anyone know what I'm doing wrong or what I could try??

    H 1 Reply Last reply
    0
    • R Reinier van de Wetering

      I'm trying this code, but I get the error :" Object reference not set to an instance of an object". The code: // Create new Table Style DataGridTableStyle ts = new DataGridTableStyle(); ts.MappingName = "Employees"; this.dataGrid1.TableStyles.Clear(); this.dataGrid1.TableStyles.Add(ts); // Assign New Width to DataGrid column this.dataGrid1.TableStyles["Employees"].GridColumnStyles["EmployeeName"].Width = newwidth; I set the DataSource for the Datagrid to dataset.Employees on the DataGrid Properties. Does anyone know what I'm doing wrong or what I could try??

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      You actually have to add a new DataGridColumnStyle before you can refer to it. If you step through your code in the debugger, you'll notice that .GridColumnStyles["EmployeeName"] name is null - you never created such a column. You have to create all the DataGridColumnStyles you want mapped, the DataGridTableStyle, add the columns to the table style's GridColumnStyles collection property, then add the table to the DataGrid's TableStyles collection property.

      Microsoft MVP, Visual C# My Articles

      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