Connecting Tech Pros Worldwide Help | Site Map

Make all the rows height even in DataGridView in .Net 2.0

  #1  
Old June 7th, 2007, 11:37 AM
Member
 
Join Date: May 2007
Location: Chennai
Posts: 99
This article explains how to make all the rows height the same in DataGridView in .Net 2.0 while using AutoSizeRowMode

In DataGridView 2.0,there is a option to resize row to fit the content of the cell.This will increase the row height.
Expand|Select|Wrap|Line Numbers
  1. this.dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCells;
But the row heights will not be the same through out the DataGridView (some row's height will be smaller than another row's height )

To make all of the rows in your DataGridView appear the same size, take the following the steps to set the max row height and set it to be the height for all the rows in dataGridView:
Step 1:
Find out the maximum row height and set the height to DataGridView preferred rowheight
Expand|Select|Wrap|Line Numbers
  1. void dataGridView1_RowHeightChanged(object sender, DataGridViewRowEventArgs e)
  2.       {
  3.  
  4.          int rowHeight = e.Row.Height;
  5.          if (this.dataGridView1.RowTemplate.Height < rowHeight)
  6.          {
  7.             this.dataGridView1.RowTemplate.Height = rowHeight;
  8.  
  9.          }
  10.     }
  11.  
Step 2
Set the AutoSizeRowsMode to none,then apply the maximum row height to all the rows
Expand|Select|Wrap|Line Numbers
  1.   private void dgLocation_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
  2.       {
  3.          int rowHeight=this.dataGridView1.RowTemplate.Height;
  4.  
  5.          this.dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
  6.          int numRows = this.dataGridView1.Rows.Count;
  7.          for (int i = 0; i < numRows; i++)
  8.          {
  9.             this.dataGridView1.Rows[i].Height = rowHeight;
  10.  
  11.          }
  12.  
  13.       }
  14.  



  #2  
Old August 17th, 2007, 04:45 PM
Member
 
Join Date: Jul 2007
Posts: 38

re: Make all the rows height even in DataGridView in .Net 2.0


How do you call these methods in a windows form?
  #3  
Old November 27th, 2007, 08:10 AM
Member
 
Join Date: May 2007
Location: Chennai
Posts: 99

re: Make all the rows height even in DataGridView in .Net 2.0


These are the datagridview events.

this.dataGridView1.RowHeightChanged += new System.Windows.Forms.DataGridViewRowEventHandler(t his.dataGridView1_RowHeightChanged);

this.dataGridView1.RowPostPaint += new System.Windows.Forms.DataGridViewRowPostPaintEvent Handler(dataGridView1_RowPostPaint);
  #4  
Old March 14th, 2008, 08:15 PM
Newbie
 
Join Date: Mar 2008
Location: delhi
Posts: 6

re: Make all the rows height even in DataGridView in .Net 2.0


how we connect a database to datagrid view in vb.net language??
Reply