Results 1 to 4 of 4

Thread: How to resize cell in DataGridView

  1. #1
    Join Date
    Feb 2009
    Posts
    78

    How to resize cell in DataGridView

    hie,

    I want to resize the height or the size of the cell in DataGridView in VB.Net. Can anybody please provide what are the functions for resizing the cell in DataGridView ?
    please help......

  2. #2
    Join Date
    Feb 2008
    Posts
    1,852

    Re: How to resize cell in DataGridView

    To set the row height in DataGridView, here's the code below -

    Code:
    public void AutoSizeGrid() 
    { 
    // DataGrid should be bound to a DataTable for this part to 
    // work. 
    int numRows = ((DataTable)gridTasks.DataSource).Rows.Count; 
    Graphics g = Graphics.FromHwnd(gridTasks.Handle); 
    StringFormat sf = new StringFormat(StringFormat.GenericTypographic); 
    SizeF size; 
    
    
    // Since DataGridRows[] is not exposed directly by the DataGrid 
    // we use reflection to hack internally to it.. There is actually 
    // a method get_DataGridRows that returns the collection of rows 
    // that is what we are doing here, and casting it to a System.Array 
    MethodInfo mi = gridTasks.GetType().GetMethod("get_DataGridRows", 
    BindingFlags.FlattenHierarchy | BindingFlags.IgnoreCase | BindingFlags.Instance 
    | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static); 
    
    
    System.Array dgra = (System.Array)mi.Invoke(gridTasks,null); 
    
    
    // Convert this to an ArrayList, little bit easier to deal with 
    // that way, plus we can strip out the newrow row. 
    ArrayList DataGridRows = new ArrayList(); 
    foreach (object dgrr in dgra) 
    { 
    if (dgrr.ToString().EndsWith("DataGridRelationshipRow")==true) 
    DataGridRows.Add(dgrr); 
    } 
    
    
    // Now loop through all the rows in the grid 
    for (int i = 0; i < numRows; ++i) 
    { 
    // Here we are telling it that the column width is set to 
    // 400.. so size will contain the Height it needs to be. 
    size = g.MeasureString(gridTasks[i,1].ToString(),gridTasks.Font,400,sf); 
    int h = Convert.ToInt32(size.Height); 
    // Little extra cellpadding space 
    h = h + 8; 
    
    
    // Now we pick that row out of the DataGridRows[] Array 
    // that we have and set it's Height property to what we 
    // think it should be. 
    PropertyInfo pi = DataGridRows[i].GetType().GetProperty("Height"); 
    pi.SetValue(DataGridRows[i],h,null); 
    
    
    // I have read here that after you set the Height in this manner that you should 
    // Call the DataGrid Invalidate() method, but I haven't seen any prob with not calling it.. 
    
    
    } 
    
    
    g.Dispose(); 
    }

  3. #3
    Join Date
    Apr 2008
    Posts
    1,948

    Re: How to resize cell in DataGridView

    There is an option to resize row to fit the content of the cell in DataGridView 2.0. This will increase the row height.

    this.dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCells;

  4. #4
    Join Date
    May 2008
    Posts
    2,012

    Re: How to resize cell in DataGridView

    If you want all the rows in your DataGridView to be appeared as the same size, follow the steps -

    Step 1: Find out the maximum row height and set the height to DataGridView preferred rowheight
    Code:
    void dataGridView1_RowHeightChanged(object sender, DataGridViewRowEventArgs e)
          {
     
             int rowHeight = e.Row.Height;
             if (this.dataGridView1.RowTemplate.Height < rowHeight)
             {
                this.dataGridView1.RowTemplate.Height = rowHeight;
     
             }
        }

    Step 2: Set the AutoSizeRowsMode to none,then apply the maximum row height to all the rows
    Code:
    private void dgLocation_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
          {
             int rowHeight=this.dataGridView1.RowTemplate.Height;
     
             this.dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
             int numRows = this.dataGridView1.Rows.Count;
             for (int i = 0; i < numRows; i++)
             {
                this.dataGridView1.Rows[i].Height = rowHeight;
     
             }
     
          }

Similar Threads

  1. How to use datagridview readonly cell
    By Ground 0 in forum Windows Software
    Replies: 3
    Last Post: 29-07-2009, 12:44 PM
  2. .net datagridview mouse position
    By Jevin in forum Software Development
    Replies: 2
    Last Post: 29-06-2009, 06:12 PM
  3. Datagridview does not detect scrollbars
    By Fragman in forum Software Development
    Replies: 2
    Last Post: 10-06-2009, 01:37 PM
  4. Disable cell validation on form DataGridView
    By Chandrakant81 in forum Software Development
    Replies: 3
    Last Post: 06-02-2009, 05:52 PM
  5. Difference Between DataGrid and DataGridView In VB.Net
    By Gallard in forum Software Development
    Replies: 3
    Last Post: 17-01-2009, 05:08 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,864,655.50410 seconds with 16 queries