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. Automating the double click required to resize a datagrids columns

Automating the double click required to resize a datagrids columns

Scheduled Pinned Locked Moved C#
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.
  • C Offline
    C Offline
    c _keithy
    wrote on last edited by
    #1

    Having got fed up of having to continually double click my way through each of the columns in my DataGrid component, is there anyway of either.. 1) Automatically getting the datagrid to resize its columns to the data or... 2) Automating this double click at the join of all the columns in a datagrid Cheers

    Give me strength, give me caffeine

    E A 2 Replies Last reply
    0
    • C c _keithy

      Having got fed up of having to continually double click my way through each of the columns in my DataGrid component, is there anyway of either.. 1) Automatically getting the datagrid to resize its columns to the data or... 2) Automating this double click at the join of all the columns in a datagrid Cheers

      Give me strength, give me caffeine

      E Offline
      E Offline
      ElSpinos
      wrote on last edited by
      #2

      Hey Keith, There is a private method in the DataGrid control class that you can’t access directly through coding, however, all is not lost as Reflection can assist you here. I’ve done this before and implemented however I can’t find the souce I used so here is a brain dump from what I can remember:

       // You'd probably want to do this every time the data grid changes it's data
       // so implement the DataSourceChanged event...
       private void MyDataGrid_DataSourceChanged(object sender, System.EventArgs e)
       {
        try
        {
         // First lets get the assembly's type reference...
         Type assemblyType = MyDataGrid.GetType();
         // The DataGrid has a method called ColAutoResize (declared as private and no-one knows why)
         // We can invoke this by getting a reference to it in the MethodInfo class.
         MethodInfo methodInfo = assemblyType.GetMethod("ColAutoResize", BindingFlags.NonPublic);
      
         // Iterate through your columns...
         for (int i = MyDataGrid.FirstVisibleColumn; (i < MyDataGrid.VisibleColumnCount); i++)
         {
          // Invoke the ColAutoResize method, the method expects a single integer
          // column index as the parameter, so we’ll pass I as an argument in the
          // object[] array.
          methodInfo.Invoke(MyDataGrid, new object[] { i });
         }
        }
        catch (Exception ex)
        {
         // Something went horribly wrong, examine the exception and deal 
         // with it accordingly or just simply ignore it, this shouldn’t 
         // happen anyhow...
        }
       }
      

      I hope this gets you furthur in your project, happy reflectioning! :)

      Fernando Mendes Senior .NET Developer, Architect

      1 Reply Last reply
      0
      • C c _keithy

        Having got fed up of having to continually double click my way through each of the columns in my DataGrid component, is there anyway of either.. 1) Automatically getting the datagrid to resize its columns to the data or... 2) Automating this double click at the join of all the columns in a datagrid Cheers

        Give me strength, give me caffeine

        A Offline
        A Offline
        AETaylor
        wrote on last edited by
        #3

        dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells

        E 1 Reply Last reply
        0
        • A AETaylor

          dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells

          E Offline
          E Offline
          ElSpinos
          wrote on last edited by
          #4

          Sometimes we try to think harder than the task at hand requires us to think. Grundyfoot's solution is much more practical... :)

          Fernando Mendes Senior .NET Developer, Architect

          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