469,306 Members | 1,896 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Share your developer knowledge by writing an article on Bytes.

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

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
4 18889
How do you call these methods in a windows form?
Aug 17 '07 #2
Vidhura
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
how we connect a database to datagrid view in vb.net language??
Mar 14 '08 #4
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

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by scottyman | last post: by
1 post views Thread by Luca Beretta | last post: by
4 posts views Thread by lord.zoltar | last post: by
4 posts views Thread by craig.wenger | last post: by
5 posts views Thread by =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?= | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.