By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,685 Members | 1,243 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

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

P: 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.  
Jun 7 '07 #1
Share this Article
Share on Google+
4 Comments


P: 38
How do you call these methods in a windows form?
Aug 17 '07 #2

P: 99
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);
Nov 27 '07 #3

P: 6
how we connect a database to datagrid view in vb.net language??
Mar 14 '08 #4

P: 1
I would suggest to change RowPostPaint in following way:
Expand|Select|Wrap|Line Numbers
  1. this.dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
  2. foreach (DataGridViewRow row in dataGridView1.Rows)
  3.    if (row.Height != rowHeight) row.Height = rowHeight;
  4.  
Apr 14 '10 #5